Sql-alchemy Integrity Error
I have User model which is described as below:- class User(db.Model, object): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(
Solution 1:
A hack would be to query the maximum id from id column and increment it by 1 and assign it to the new user to prevent primary key collision.
max_id = session.query(User).order_by(User.id.desc()).first()
new_user =User(id=max_id+1, email=form.email.data, username=form.username.data, password=hashed_password)
db.session.add(new_user)
db.session.commit()
Solution 2:
Just use a Sequence to auto-increment your id.
from sqlalchemy import Integer, Sequenceid = Column(Integer, Sequence('id_seq'), primary_key=True)
Post a Comment for "Sql-alchemy Integrity Error"