这个断言是否总是通过?换句话说,当向会话添加新对象时,SQLAlchemy 是否保存顺序(在生成 INSERT 查询时)?
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.engine import create_engine
from sqlalchemy.types import Integer
from sqlalchemy.schema import Column
engine = create_engine('sqlite://')
Base = declarative_base(engine)
Session = sessionmaker(bind=engine)
session = Session()
class Entity(Base):
__tablename__ = 'entity'
id = Column(Integer(), primary_key=True)
Entity.__table__.create(checkfirst=True)
first = Entity()
session.add(first)
second = Entity()
session.add(second)
session.commit()
assert second.id > first.id
print(first.id, second.id)
没有,在生产中我使用的是 postgresql,sqlite 用于测试。