当我运行以下代码时,我期望first_name和last_name成为一个复合主键,并且id成为行的自增索引,但不要作为主键,因为表中其余信息是我定义它唯一性所需的,而不是给定的ID。
当我在DataGrip中查看结果时,出现以下表格。数据未按添加顺序排列,id列为空,而不是预期的自增整数。 简单明了地说:我的问题是:如何为一个不是主键的SQLAlchemy ORM类创建自增索引?
Base = declarative_base()
Session = sessionmaker(bind=db)
session = Session()
class Person(Base):
__tablename__ = "people"
id = Column(Integer, index=True, unique=True, autoincrement=True, primary_key=False)
first_name = Column(String(30), primary_key=True)
last_name = Column(String(30), primary_key=True)
if __name__ == "__main__":
Base.metadata.create_all(db)
session.add_all([
Person(first_name="Winston", last_name="Moy"),
Person(first_name="Bill", last_name="Gates"),
Person(first_name="Steve", last_name="Jobs"),
Person(first_name="Quinten", last_name="Coldwater")
])
session.commit()
当我在DataGrip中查看结果时,出现以下表格。数据未按添加顺序排列,id列为空,而不是预期的自增整数。 简单明了地说:我的问题是:如何为一个不是主键的SQLAlchemy ORM类创建自增索引?