I am using sqlite database and I declared the models as in this gist
https://gist.github.com/mmahesh/7245561
I have added a model instance with transaction manager as
with transaction.manager:
model = Users(username='example',name='Example', email_primary='m@m.com', _password='example')
DBSession.add(model)
Whenever I execute initialize_sample_db development.ini, this error shows up
sqlalchemy.exc.IntegrityError: (IntegrityError) constraint failed 'INSERT INTO users (name, username, email_primary, email_secondary, _password, bio) VALUES (?, ?, ?, ?, ?, ?)' ('Example', 'example', 'm@m.com', None, 'example', None )
Additional Information: when i use sqlite3 command tool and after '.d' command i get
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
COMMIT;
Thanks in advance
In table users
you have defined column id
as primary key.
However, in your INSERT
statement you do not provide id
.
That would be totally fine if id
was auto-increment column - but it is not.
Solution is to add sqlite_autoincrement=True
into your users
table definition.
Also, your users
table definitions seems to be too strict - you require most fields to be non-nullable. It depends on your task, but sometimes being too strict can be bad idea - it will also cause constraint violation if you forgot (or simply don't want) to fill one of those fields.
来源:https://stackoverflow.com/questions/19700697/struck-at-sqlalchemy-exc-integrityerror-integrityerror-constraint-failed-ins