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.
source
share