question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't understand how pull data from joins

See original GitHub issue

So 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:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
coleifercommented, Aug 2, 2019

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.

0reactions
karambaqcommented, Aug 2, 2019

@coleifer Oh, let me show that:

    games = (Fighter.select(Fighter, Timestamp).
             join(Timestamp, on=(Fighter.id == Timestamp.id))
             )
    print(games[0].timestamp.timestamp) # This prints fine
    for g in games.namedtuples(): # There is an error
        print(g)

Traceback:

 File "/usr/local/lib/python3.7/site-packages/peewee.py", line 4127, in next
    self.cursor_wrapper.iterate()
  File "/usr/local/lib/python3.7/site-packages/peewee.py", line 4043, in iterate
    self.initialize()  # Lazy initialization.
  File "/usr/local/lib/python3.7/site-packages/peewee.py", line 7065, in initialize
    self.tuple_class = collections.namedtuple('Row', attributes)
  File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/collections/__init__.py", line 373, in namedtuple
    raise ValueError(f'Encountered duplicate field name: {name!r}')
ValueError: Encountered duplicate field name: 'id'

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 of games.namedtuples() it works just as expected.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found