My model is quite complex and I'm trying to take logic from existing stored procedures and convert them to SQLAlchemy (for portability reasons).
I'm struggling however with uncommitted data.
I have
user
status
user_statuses
user = User(name = 'Test')
status = Status(name = 'Active')
db.session.add(user)
db.session.add(status)
# Oooopa! This is where it fails
user_session = UserStatuses(user_id=user.id, status_id=status.id, datetime.utcnow(), datetime(9999,01,01,00,00,00))
# both user.id and status.id = None as it's uncommited!
class User(Base):
....
@staticmethod
def prefetch_id():
db.session.execute("SELECT NEXTVAL('user_id_seq');").scalar()
If you flush the session after adding model objects but before committing:
db.session.add(user)
db.session.add(status)
db.session.flush()
then the objects add()
-ed will get their sequence columns (id
) updated so user.id
, status.id
won't be None
any more.