I'm using MySQL 8.0 and SQLAlchemy. My id column isn't incrementing, and I don't understand why.
SQLAlchemy Model:
class Show(db.Model):
__tablename__ = "shows"
id = Column(Integer, primary_key=True, index=True)
name = Column(String)
type = Column(String)
status = Column(String)
episodes = Column(Integer)
series_entry_id = Column(Integer, ForeignKey("series.id"))
series_id = Column(Integer, ForeignKey("series.id"))
lists = relationship("List", secondary=show_list, back_populates="shows")
recommendations = relationship("Recommendation", backref=backref("shows"))
user_ratings = relationship("Rating", backref=backref("shows"))
alt_names = relationship("User", secondary=alt_names, back_populates="alt_show_names")
series_entry = relationship("Series", foreign_keys=[series_entry_id], uselist=False)
series = relationship("Series", foreign_keys=[series_id], post_update=True)
Breaking code:
show = Show(
name=new_data["title"]["english"],
type=new_data["format"],
status=new_data["status"],
episodes=new_data["episodes"],
)
db.session.add(show)
db.session.commit()
The original error I received was:
sqlalchemy.exc.DatabaseError: (mysql.connector.errors.DatabaseError) 1364 (HY000):
Field 'id' doesn't have a default value
From this answer, I added the index parameter to my id column and edited the my.ini file to take it out of STRICT_TRANS_TABLES
mode. The new error is:
sqlalchemy.exc.IntegrityError: (mysql.connector.errors.IntegrityError) 1062 (23000):
Duplicate entry '0' for key 'shows.PRIMARY'
All answers I've found on the topic talk about AUTO_INCREMENT
, but the SQLAlchemy docs say that that should be the default here, given that it's an integer primary key without it specified to false. I did try adding autoincrement=True
just in case, but when I tried to migrate it alembic told me that no changes were detected.