News for hackers. Stuff that matters.
Re:SQLAlchemy 2.0 Released by v3ss0n on Friday January 27, 2023 @12:06AM (#34542710)
Then one really simple question: what to use ?
Re:SQLAlchemy 2.0 Released by dragonwriter on Friday January 27, 2023 @12:34AM (#34542875)
If you are doing ORM on a greenfield project, the declarative style.
Re:SQLAlchemy 2.0 Released by v3ss0n on Friday January 27, 2023 @12:45AM (#34542957)
Which declarative style? Mapped or columns?
Re:SQLAlchemy 2.0 Released by zo1 on Friday January 27, 2023 @01:14AM (#34543160)
Columns. Even early on many years ago, I ignore all the exotic forms and different ways. Leave those for when you need to do something non-standard or programmatic.

Choose a form that works for you and looks nice, apply it to all your oddest use cases like many to many relationships and self-referential relationships, and then use that as a template going forward.

Re:SQLAlchemy 2.0 Released by BerislavLopac on Friday January 27, 2023 @04:32AM (#34544240)
My recommendation is to avoid using an ORM (SQLAlchemy or any other) to define the business logic models (which is the approach used in Django). That works well only if your business models and your database entities align very closely, but in many large systems this is not the case, end you end up running around in circles trying to align the two.

Instead, use either an ORM, or even better the SQLALchemy core, to manage and abstract out the database; and use something like dataclasses, attrs or Pydantic to define your business logic models. In a network-oriented application, i.e. an API, I would recommend to use a separate tool to model the I/O entities, again unless they correspond very closely to the business ones.

Re:SQLAlchemy 2.0 Released by Spivak on Friday January 27, 2023 @10:25AM (#34547036)
So build your own application-specific bespoke ORM from dataclasses rather than using the one sitting right there?
Re:SQLAlchemy 2.0 Released by travisjungroth on Friday January 27, 2023 @10:37PM (#34554883)
When that pattern is done well, and it can be, it’s that you have a layer that doesn’t care that stuff is stored in a database. It’s lots of logic. That layer uses tools like dataclasses.

Then you have some layer that stores things in the database. It’s like a record keeper. It uses an ORM or not.

One benefit of this is ORMs are giant, complex objects by necessity. It’s good to only use them where they’re needed, at the database. Then you don’t have all their magic floating through your business code. They’re also rather opinionated. I, at least, experience whay more freedom when making a class from scratch than using an ORM.

This comes at a cost of maintaining two versions of lots of things and a translation layer in between. It is overkill for super CRUDy stuff (most apps).

Re:SQLAlchemy 2.0 Released by zzzeek on Friday January 27, 2023 @10:55AM (#34547414)
the current tutorial and quickstart guide should make it exceedingly clear, how is it that people are still confused?

this is the quickstart:

https://docs.sqlalchemy.org/en/20/orm/quickstart.html

that's the one you use. That's it! There is no other style listed there

lots of people still want to use other styles or still have to because they have legacy code. So these are documented also. However, SQLAlchemy wants you to use the style at:

https://docs.sqlalchemy.org/en/20/orm/quickstart.html

that's the only style we would have if we were starting now.