News for hackers. Stuff that matters.
Re:SQLAlchemy 2.0 Released by zzzeek on Thursday January 26, 2023 @08:23PM (#34541315)
I would urge people who have had issues with the documentation to give the 2.0 documentation a try. Many aspects of it have been completely rewritten, both to correctly describe things in terms of the new APIs as well as to modernize a lot of old documentation that was written many years ago.

First off, SQLAlchemy's docs are pretty easy to get to, for a direct link just go to:

https://docs.sqlalchemy.org/

It's an esoteric URL I know! ;)

from there, docs that are new include:

- the Quickstart, so one can see in one quick page what SQLAlchemy usually looks like: https://docs.sqlalchemy.org/en/20/orm/quickstart.html

- the Unified Tutorial, which is a dive into basically every important concept across Core / ORM : https://docs.sqlalchemy.org/en/20/tutorial/index.html

- the ORM Querying guide, which is a "how to" for a full range of SQL generation: https://docs.sqlalchemy.org/en/20/orm/queryguide/index.html

it's still a lot to read of course but part of the idea of SQLAlchemy 2.0 was to create a straighter and more consistent narrative, while it continues to take on a very broad-based problem space. If you compare the docs to those of like, PostgreSQL or MySQL, those docs have a lot of sections and text too (vastly more). It's a big library.

Re:SQLAlchemy 2.0 Released by mixmastamyk on Thursday January 26, 2023 @08:59PM (#34541555)
Great tool. I think this is why folks have trouble finding the docs:

Whole entire site is docs everywhere, including home page. Links to "docs" are tiny on the home page. Main menu calls them "library" instead of docs, why?

Majority of the stuff is focused on migration rather than new users. These two should be separated into separate tracks immediately. There is an attempt, but it gets lost somehow.

Also the release page linked here and "what's new in 2.0" go straight into the deep end, "inside baseball" style. These kind of pages should be very light. Tons of history-I'm personally not interested when trying to get something done. Unsolicited advice, move that into another blog post. ;-)

Re:SQLAlchemy 2.0 Released by zzzeek on Thursday January 26, 2023 @09:41PM (#34541812)
The link I posted here is the blog post. That's why it's historical and chatty, it's the blog post.
Re:SQLAlchemy 2.0 Released by inquirer39243 on Thursday January 26, 2023 @09:52PM (#34541890)
Love your tools!

I think you should consider changing "library" to "documentation" - for whatever reason I also struggled to discover "library" actually meant docs - but only after I also clicked on a specific version.

I think the best UI/UX for this is something like django's [0], where "documentation" brings you straight to the latest version's documentation.

[0] https://www.djangoproject.com/

Re:SQLAlchemy 2.0 Released by zzzeek on Friday January 27, 2023 @10:49AM (#34547340)
> I think you should consider changing "library" to "documentation" - for whatever reason I also struggled to discover "library" actually meant docs - but only after I also clicked on a specific version.

you got it, look now

> I think the best UI/UX for this is something like django's [0], where "documentation" brings you straight to the latest version's documentation.

the design of our main docs index page was taken completely from how Django does it (e.g. how they have "paragraphs of links" at https://docs.djangoproject.com/en/4.1/)

Re:SQLAlchemy 2.0 Released by inquirer39243 on Friday January 27, 2023 @08:24PM (#34554119)
Oh yeah I love it!

I don't know why but it's just way more intuitive this way for me at least, thanks!

Re:SQLAlchemy 2.0 Released by zzzeek on Friday January 27, 2023 @10:46AM (#34547318)
> Whole entire site is docs everywhere, including home page. Links to "docs" are tiny on the home page.

you got it, look now. If you can't find the docs now, I dont know what to say.

> Main menu calls them "library" instead of docs, why?

A library is where you'd go to learn...but I guess, because the product is a "library", that's why it's not obvious? The term is gone from the nav etc. the sub-page is still called "library" but nobody needs to go there.

Re:SQLAlchemy 2.0 Released by mmerickel on Thursday January 26, 2023 @10:38PM (#34542166)
SQLAlchemy and your efforts are so amazing and appreciated. Thank you and the team for all of your hard work on 2.0 as well as the heroic effort that was put into 1.4 as a stepping stone for existing codebases!
Re:SQLAlchemy 2.0 Released by Dowwie on Friday January 27, 2023 @09:21AM (#34546309)
Hey Mike! :) Hope all is well.
Re:SQLAlchemy 2.0 Released by westurner on Thursday January 26, 2023 @10:50PM (#34542228)
Pandas had a docs sprint awhile back. Are the DOCs issues labeled?
Re:SQLAlchemy 2.0 Released by v3ss0n on Thursday January 26, 2023 @11:38PM (#34542540)
So confused after reading unified tutorial. It's so confusing and too many styles to declare ORM

>>> class User(Base): ... __tablename__ = "user_account" ... ... id: Mapped[int] = mapped_column(primary_key=True) ... name: Mapped[str] = mapped_column(String(30)) ... fullname: Mapped[Optional[str]] ... ... addresses: Mapped[List["Address"]] = relationship(back_populates="user") ... ... def __repr__(self) -> str: ... return f"User(id={self.id!r}, name={self.name!r},

VS

class User(Base): __tablename__ = "user_account"

    id = mapped_column(Integer, primary_key=True)
    name = mapped_column(String(30), nullable=False)
    fullname = mapped_column(String)

    addresses = relationship("Address", back_populates="user")

    # ... definition continues

Vs

user_table = Table( "user", mapper_registry.metadata, Column("id", Integer, primary_key=True), Column("name", String(50)), Column("fullname", String(50)), Column("nickname", String(12)), )

Vs

class Customers(Base): __tablename__ = 'customers'

   id = Column(Integer, primary_key = True)
   name = Column(String)
   address = Column(String)
   email = Column(String)
Why so many styles? Why can't be like pydantic or Django and use just one style for ORM?

it's breaking zen of python, it makes searching examples on Google very hard ( esp with Google getting really crappy)

Re:SQLAlchemy 2.0 Released by dragonwriter on Friday January 27, 2023 @12:02AM (#34542677)
> Why can't be like pydantic

Pydantic also has multiple styles, in fact, I think it has more styles than SQLAlchemy.

> and use just one style for ORM?

To be fair, one of the “styles” you show is not ORM, its the lower-level Core which can be used without the ORM and which the ORM is built on top of. And the two ORM styles include an older one largely for backward compatibility and a more modern one.

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.

Re:SQLAlchemy 2.0 Released by zzzeek on Friday January 27, 2023 @10:53AM (#34547395)
I can't see the difference between the first two "VS" examples you have above, it just looks like they are formatted differently (oh, one has annotations and the other doesn't. you'd use the annotated version. I guess you are cut-and-pasting from a migration guide that's for legacy code. "migration guide" means "I have legacy code". if you don't have legacy code, you have nothing to migrate. Go straight to https://docs.sqlalchemy.org/en/20/orm/quickstart.html ).

The second example is a Core table. The new tutorial makes it clear that this is an underlying element that nonetheless you have to sometimes create directly.

the third example is gone. That style is not in the docs anymore *except* in a clearly marked side-box that is talking about a legacy version of SQLAlhcemy - you don't have to read that and the box IMO makes it as clear as it can that this is not the current way of doing things.

> Why so many styles? Why can't be like pydantic or Django and use just one style for ORM?

pydantic is extremely new and SQLAlchemy's declarative style pre-dates Python 3 entirely. So we have changed it. The old ways of doing things have to be maintained for backwards compatibility with millions of existing applications. You can't be around for 16 years and have exactly one way of doing things that never changes, legacy patterns for large software libraries are a fact of life.

as for django I dont know what they are doing, I am sure things had to change for them to accommodate pep-484 typing in models (if they have even done that).

Re:SQLAlchemy 2.0 Released by elephantum on Friday January 27, 2023 @12:11AM (#34542726)
Just want to thank you for all the work you do! We are heavy users of sqlalchemy, our life would be much harder without it!
Re:SQLAlchemy 2.0 Released by zzzeek on Friday January 27, 2023 @10:58AM (#34547456)
youre welcome, glad it is of help
Re:SQLAlchemy 2.0 Released by robertlagrant on Friday January 27, 2023 @07:12AM (#34545158)
This has been a long time coming. Many congratulations.
Re:SQLAlchemy 2.0 Released by thisismyswamp on Monday January 30, 2023 @07:14AM (#34578686)
As a first time reader of these docs, they don't seem at all approachable.

For example: for the quickstart, the premise is simple - show me quickly how I can create a class, map it to the database, create some examples, and delete them. Something like what mongoose does here: https://mongoosejs.com/docs/

Instead, SQLAlchemy goes from how to declare models (with a wall of code followed by a wall of text), jumps into creating "engines", shows some generated SQL code for creating tables, and so on.

Sessions are simply used without being introduced, and many questions arise - am I hoarding a database connection as long as I am holding onto the session? Why should I do anything other than executing queries while that is happening? Then that means the `with` expression is useless, unless I want to explicitly run a multiple-query transaction - but why should this be the default?

It also comes across as clunky to be importing a `select` statement at the top level. Why not call select on the table objects themselves? And what's up with `select(...).select_from(...)`?