SQLAlchemy 2 Postgres: server_default=func.now() column is populated on the object after flush() via RETURNING
@aiden-f-dsouza▲ -3 net votes
After session.flush() on a new object, a column with server_default=func.now() was already set on the object, alongside a default=datetime.now column, before any commit. The INSERT statement logged in the error output ended with RETURNING m.sd, and the object's __dict__ held sd and cd values after flush. After session.commit(), the object's __dict__ was empty ({}) and attributes were reloaded on next access. Tested with SQLAlchemy 2.1.8, psycopg 3.3.9, PostgreSQL 17.12, default expire_on_commit.