coleifer / peewee

a small, expressive orm -- supports postgresql, mysql, sqlite and cockroachdb
http://docs.peewee-orm.com/
MIT License
11.2k stars 1.37k forks source link

Question about a Get on a table with foreignkey relationtionship #2834

Closed accedic closed 9 months ago

accedic commented 9 months ago

I have tried looking at the documention and cant seem to find anything about performing a get on a table with a foreingkey relation. So my question is simple. Can a get be performed on a table with a foreing key and since in my case its a one to one relationship, will it work and would it return the content of both tables joined as one result?

Thanks

coleifer commented 9 months ago

Can you please give a concrete example of what you’re trying to do?

accedic commented 9 months ago

So unless theres something im getting totally wrong, i would expect the result variable below to return a single row joining the 2 tables toghether. As a user had only a single row possible in each table. I wrote this example really quick just for the sake of understanding what i'm looking for. I manage to make my code work with a select, however i need to iterate over the list that is returned by select. Being able to perform a get that would join the 2 rows from the 2 tables would avoid me to iterate over the list in order to achieve the same end goal. I hope this example clears up what i trying to achieve.

Thanks in advance.

Example below:

class BaseModel(Model): class Meta: database = myDB table1 = "table1" table2 = "table2"

class table1(BaseModel): id = PrimaryKeyField() name = TextField(unique=True) active = BooleanField()

class table2(BaseModel): name = ForeignKeyField(table1, column_name='name') lastName = TextField() email = TextField()

result = table2.get(table2.name=="bob") print(model_to_dict(result))

{'table1.id' :1, 'name': 'bob', 'active' = True, 'lastName': 'saget', 'email': 'bob.saget@gmail.com' }

accedic commented 9 months ago

Forget about this. I figured it out. Thanks

coleifer commented 9 months ago

Please take the time to learn to format your code when commenting, it makes things a lot easier.

Also your example isn't even correct or representative of what actually would print...

Also please read the docs.

If you actually typed-in some code like yours, you would see:

from peewee import *
from playhouse.shortcuts import *

db = SqliteDatabase(':memory:')

class T1(db.Model):
    name = TextField(unique=True)
    active = BooleanField()

class T2(db.Model):
    name = ForeignKeyField(T1)
    email = TextField()

db.create_tables([T1, T2])
t1 = T1.create(name='t1', active=True)
t2 = T2.create(name=t1, email='t2@example.com')

obj = T2.get(T2.id == t2.id)
print(model_to_dict(obj))

Prints:

{'id': 1, 'name': {'id': 1, 'name': 't1', 'active': True}, 'email': 't2@example.com'}
accedic commented 9 months ago

@coleifer Thanks for the answer. Yeah i had forgot to code block my example. I had just wrotte it for the example, its not something i actually tested to see if it was writen properly. However, your first response triggered me to relook into it properly and i actually got working. Thanks for taking the time to answer tho.