Can't understand how pull data from joins
See original GitHub issueSo I have two tables:
fighters
id SERIAL PRIMARY KEY,
left_fighter text,
right_fighter text
timestamp
id SERIAL PRIMARY KEY,
timestamp text,
daytime time
Code
class BaseModel(Model):
class Meta:
database = db
class Fighter(BaseModel):
id = PrimaryKeyField(null=False)
left_fighter = CharField()
right_fighter = CharField()
class Meta:
table_name = 'fighters'
class Timestamp(BaseModel):
id = PrimaryKeyField(null=False)
timestamp = TimestampField()
daytime = TimeField()
table_name = 'timestamp'
fighter = (Fighter.select().join(
Timestamp, on=(Fighter.id == Timestamp.id)).first())
print(fighter.__dict__)
Output:
{'__data__': {'id': 201322952, 'left_fighter': 'Кенши', 'right_fighter': 'Лю Кенг'}, '_dirty': set(), '__rel__': {}}
Question
So I see there is data from first table, but I can’t understand how to pull second table data.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
SQL joins and how to use them - Launch School
The JOIN is based on the conditions supplied in the ON clause. A LEFT JOIN will always include the rows from the LEFT...
Read more >I cannot get this LEFT JOIN to work (I don't understand joins)
My limited understanding was that LEFT join is what I needed. I want data from the left table even if there is no...
Read more >A step-by-step walkthrough of SQL Inner Join - SQLShack
Inner Join clause in SQL Server creates a new table (not physical) by combining rows that have matching values in two or more...
Read more >SQL JOIN Tips for Beginners | LearnSQL.com
The INNER JOIN clause will return only matching records that are the same in both tables. Note that INNER JOIN is the same...
Read more >Join tables and queries - Microsoft Support
Use joins in Access queries to combine records from different data sources so that each pair of records from the sources becomes one...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The behavior is what it is, as Peewee does not handle nesting or relationship-traversal when using the dicts/namedtuples/tuples methods. You can explicitly alias Timestamp.id in the .select() call if you wish, but you need to then explicitly specify all the columns on Timestamp which you intend to select.
@coleifer Oh, let me show that:
Traceback:
Is it ok? If yes can you please explain it to me?
UPD: I’m not so good in database architecture, id fields are inserting manually, they not auto-generated, there is one-to-one relationship, should I make id column in the second table as foreign key?
UPD2: When I’m using
games.dicts()
instead ofgames.namedtuples()
it works just as expected.