Flask-SqlAlchemy Sqlite Foreign Key constraint not working?
<p>Here is my models:</p>

<pre><code>class Item(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    item_type_id = db.Column(db.Integer, db.ForeignKey('item_type.id'))
    purchase_price = db.Column(db.Integer, index=True)
    purchase_transaction_id = db.Column(db.Integer, db.ForeignKey('purchase_transaction.id'))
    supplier_id = db.Column(db.Integer, db.ForeignKey('supplier.id'))

class ItemType(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    item_type = db.Column(db.String(ITEM_TYPE_LENGTH), index=True, unique=True)
    items = db.relationship('Item', backref='item_type', lazy='dynamic')
</code></pre>

<p>So this is what I did:</p>

<pre><code>&gt;&gt;&gt; db.session.add(models.Item(item_type_id=170, purchase_price=111, purchase_transaction_id=1, supplier_id=1)) 
&gt;&gt;&gt; db.session.commit()
</code></pre>

<p>I added a new Item <code>item_type_id</code> is 170, but the table of item_type only has <code>id</code> until 20. And if I check my database, the <code>item</code> is successfuly added, and if I do a query like <code>ItemType.query.get(item.item_type_id)</code>, <code>None</code> will be returned - which means the foreign key does not exist</p>

<p>I have read the docs in <a href="http://docs.sqlalchemy.org/en/improve_toc/core/constraints.html" rel="nofollow noreferrer">sqlalchemy</a> and <a href="https://www.sqlite.org/foreignkeys.html" rel="nofollow noreferrer">sqlite</a>, they both should have foreign key constraint, I wonder why it is not working in mine</p>