Foreign Key Constraints in SQLAlchemy

I am using the ORM side of SQLAlchemy, and I defined one of my columns to relate the foreign key to another model using:

Base = declarative_base()
class Model1(Base):
    __tablename__ = 'm1'
    Name = Column(String, primary_key = True)
    info = Column(String)

class Model2(Base):
    __tablename__ = 'm2'
    Name = Column(String, primary_key = True)
    info = Column(String)
    other_model = Column(String, ForeignKey('m1.Name'))

However, it does not seem to matter what I put in the attribute other_model, it seems more than happy to pass it to the database, even if there is no instance Model1that has this Name.

+5
source share
1 answer

It looks like the answer was in the database I used (SQLite), not SQLAlchemy. SQLite version <3.6.1 (AFAIK) does not support foreign key constraints.

Therefore, the answer is very similar to this answer to foreign keys and SQLAlchemy .

Windows, pysqlite2, 3.7.6.2 sqlite, SQLAlchemy SQL- . SO > .

, SQLite- , SO- .

+3

All Articles