Flask SQLAlchemy NOT NULL constraint failed on primary key Flask SQLAlchemy NOT NULL constraint failed on primary key sqlite sqlite

Flask SQLAlchemy NOT NULL constraint failed on primary key


Add explicit autoincrement=True to Trips class definition:

id = db.Column(db.Integer, primary_key=True, autoincrement=True)

If table is created without explicit AUTOINCREMENT, you need to pass Trips.id=NULL in order to increment it, see https://www.sqlite.org/faq.html#q1


In the version:SQLAlchemy 1.2.7Flask-SQLAlchemy 2.3.2autoincrement=True is not a required para, but be careful with your primary key definition, since you have defined 'id' column as a primary key, you should not define another column as primary key if not necessary, otherwise, you must set the value of id column by yourself when you inserting the record.


I had this same problem in a class mapped table used as a many-many with additional attribute fields. The reason the error was popping up wasn't because autoincrement wasn't explicitly defined for the primary key, but rather because the foreign_key columns also had primary_key = True. The relationship works fine with out the additional primary_keys set in the model.