Python SQLAlchemy ORM - Relationship Loading Techniques

  Рет қаралды 3,131

Zeq Tech

Zeq Tech

Күн бұрын

Пікірлер: 19
@surendrareddyGoluguri
@surendrareddyGoluguri 27 күн бұрын
Thanks Zeq for making this amazing content on youtube. Your SQL alchemy series helped me a lot to start working on this technology😊.
@zeqtech
@zeqtech 27 күн бұрын
No problem! I'm glad to hear it has helped 😎
@TheDigitalSight
@TheDigitalSight 4 күн бұрын
Thanks, Your video helped me understand SA Relationships easily ❤ Waiting for more videos on SA 😇
@zeqtech
@zeqtech 2 күн бұрын
Glad to hear that it was helpful! There is more on the way!
@AnthonyTetteh-t7q
@AnthonyTetteh-t7q 3 ай бұрын
Thanks ZT. Simple, precise and straight to the point. It'd be really really helpful if you'd added the perf_counter benchmark throughout to weigh the performance
@zeqtech
@zeqtech 3 ай бұрын
No problem! Yes 👍 That's a good idea 💡
@Melvi25
@Melvi25 7 ай бұрын
Hi ZT, thank you for great and simple tutorials. The SQLAlch docs is really hard to read for newbie... core-way, orm-way old and new.. mostly all mixed in 1 tutorial.. Will you do also tutorial for quering with joins? Getting values from Second table when you have FK (ID from 2nd table) in Main table. I think I set correctly relationship(back_populates=).. but still can't get "second.name" when quering Main table
@zeqtech
@zeqtech 7 ай бұрын
You're welcome! Yes I understand that! Yes I will be making a video on that. If you use the "back_populates" then you need to have the relationship variable in the second table too like this example on my github: github.com/ZeqTech/sqlalchemy-tutorials/blob/main/ep_06_one_to_many_relationships/user_post_example.py
@rainrope5069
@rainrope5069 6 ай бұрын
really fantastic SQLAlchemy series
@zeqtech
@zeqtech 6 ай бұрын
Thank you 👍
@tomasemilio
@tomasemilio 2 ай бұрын
Any tips for async sqlalchemy? Also when i do get posts i want select but when i do post where id equals blaaa i want all the relationships. What is the best strategy
@zeqtech
@zeqtech 2 ай бұрын
I haven't touched async SQLALchemy, when I do I can make a video on it . If you're saying you want all the relationships connected to the posts then you can do something like: # Using a selectinload() could be fast, '*' applies the loading to all relationships query = session.query(Post).options(selectinload('*')).filter(Post.id == 1).one_or_none() That will select and load all the relationships to that posts table for the specific post. You will have to play around with what is the best method, there are factors that go into it like how many relationships you're loading from. # Using a joinedload() could be faster query = session.query(Post).options(joinedload('*')).filter(Post.id == 1).one_or_none() You can also get specific to which relationships you want to load specifically: query = ( session.query(Post) .options( selectinload(Post.detail), joinedload(Post.other_relationship), ) .filter(Post.id == 1) .one_or_none() ) The best strategy is really just to find out what works for your situation
@MrSule26
@MrSule26 7 ай бұрын
Hi, Zeq Tech Thanks for the video. I've been trying using SQLAlchemy ORM with mapped_column for a project of mine and I've been having a lot of issues, but your videos are helping me a lot. Could you answer this question of mine? For example: I inserted this data on the database itauBank = Bank( name=BANK_NAMES["ITAU"], account_types=[ AccountType( name=ACCOUNT_TYPES["CHECKING_ACCOUNT"], account_rules=AccountRule( withdraw_fee=1, minimum_initial_balance=50, ), ), AccountType( name=ACCOUNT_TYPES["SAVING_ACCOUNT"], account_rules=AccountRule( withdraw_fee=3, minimum_initial_balance=200, ), ), ], ) How can I make a select which you get the object Bank with the name = "Itau" and only the AccounType with the name = "Checking Account"? I've already tried a lot of thinks, but I can just filter the name of the bank, but not both at the same time
@zeqtech
@zeqtech 7 ай бұрын
No problem! Ah I see, yeah this will be kind of a beefy response lol. You will need to join the 2 tables in order to filter on both attributes. You can do something like this: bank = session.query(Bank).join(AccountType).filter(and_(Bank.name=="Itau", AccountType.name=="Checking Account")).first() but it will return the bank object so when you access the `account_types` variable it will give you all the accounts. But if you just want to get the accounts then you can do any of these: account = session.query(AccountType).join(Bank).filter(and_(Bank.name=="Itau", AccountType.name=="Checking Account")).first() account = session.scalar(select(AccountType).join(Bank).filter(and_(Bank.name=="Itau", AccountType.name=="Checking Account"))) account = session.execute(select(AccountType).join(Bank).filter(and_(Bank.name=="Itau", AccountType.name=="Checking Account"))).first() Those only return the first entry, but if you want all entries then you can do these: accounts = session.query(AccountType).join(Bank).filter(and_(Bank.name=="Itau", AccountType.name=="Checking Account")).all() accounts = session.scalars(select(AccountType).join(Bank).filter(and_(Bank.name=="Itau", AccountType.name=="Checking Account"))).all() accounts = session.execute(select(AccountType).join(Bank).filter(and_(Bank.name=="Itau", AccountType.name=="Checking Account"))).all() if you add the: bank: Mapped["Bank"] = relationship() on the AccountType table, you can then access the bank for that specific account
@zeqtech
@zeqtech 7 ай бұрын
I also want to add as a possibility to this returning the bank object and only the checking account associated with it: query = session.query(Bank).join(Bank.account_types).options( contains_eager(Bank.account_types) ).filter(and_(Bank.name == "Itau", AccountType.name == "Checking Account")) bank = query.first() if bank: print(bank) for account_type in bank.account_types: print(account_type) The containes_eager allows you to perform those check conditions on the subquery Other than that you can get it as a tuple like this: bank_account_tuple = session.query(Bank, AccountType).join(AccountType).filter(and_(Bank.name=="Itau", AccountType.name=="Checking Account")).all() print(bank_account_tuple)
@MrSule26
@MrSule26 7 ай бұрын
@@zeqtech I see. Thanks for the tips. The first answer helped me a lot. I'm making progress now.
@tieutuyendang4545
@tieutuyendang4545 8 ай бұрын
Great tutorial! ❤👍
@zeqtech
@zeqtech 8 ай бұрын
Thank you so much babe 🤟❤️
@greob
@greob 5 ай бұрын
Nice presentation, thanks for sharing!
SQLAlchemy ORM - A NEW WAY TO CREATE COLUMNS 🆕🆕🆕
11:07
This Is Why Python Data Classes Are Awesome
22:19
ArjanCodes
Рет қаралды 810 М.
Ozoda - Lada ( Official Music Video 2024 )
06:07
Ozoda
Рет қаралды 31 МЛН
Who’s the Real Dad Doll Squid? Can You Guess in 60 Seconds? | Roblox 3D
00:34
What's in the clown's bag? #clown #angel #bunnypolice
00:19
超人夫妇
Рет қаралды 12 МЛН
Solving one of PostgreSQL's biggest weaknesses.
17:12
Dreams of Code
Рет қаралды 203 М.
JavaScript Event Loop -- Visualized!
29:43
ColorCode
Рет қаралды 14 М.
Databases & SQLAlchemy - Flask Tutorial Series #7
33:58
NeuralNine
Рет қаралды 15 М.
Собеседование Python. Разбор вопросов
2:24:59
Declarative Routes for NextJS and React-Router: DRY Routing
13:07
Jack Herrington
Рет қаралды 26 М.
Python SQLAlchemy ORM - Many to Many Relationships
14:37
Zeq Tech
Рет қаралды 4 М.
Pydantic Tutorial • Solving Python's Biggest Problem
11:07
pixegami
Рет қаралды 279 М.
SQLAlchemy ORM crash course - Learn SQLAlchemy in 1 hour
1:00:35
Demos Petsas
Рет қаралды 22 М.