Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

struck at sqlalchemy.exc.IntegrityError: (IntegrityError) constraint failed 'INSERT INTO users

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='[email protected]', _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', '[email protected]', 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

like image 225
Mahesh Chandra Mukkamala Avatar asked Oct 31 '13 07:10

Mahesh Chandra Mukkamala


1 Answers

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.

like image 183
mvp Avatar answered Oct 16 '22 12:10

mvp