4

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

1 Answers1

5

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.

mvp
  • 111,019
  • 13
  • 122
  • 148
  • personally i like to start with strong constraints and remove them when really necessary; – Lars Apr 29 '17 at 19:54