Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SQLAlchemy中的外键约束_Python_Sqlalchemy - Fatal编程技术网

Python SQLAlchemy中的外键约束

Python SQLAlchemy中的外键约束,python,sqlalchemy,Python,Sqlalchemy,我使用的是SQLAlchemy的ORM端,我定义了一个列与另一个模型具有外键关系,使用: Base = declarative_base() class Model1(Base): __tablename__ = 'm1' Name = Column(String, primary_key = True) info = Column(String) class Model2(Base): __tablename__ = 'm2' Name = Colum

我使用的是SQLAlchemy的ORM端,我定义了一个列与另一个模型具有外键关系,使用:

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'))

然而,我在
other_model
属性中输入的内容似乎并不重要,它似乎非常乐意将其提交到数据库,即使没有
Model1
实例具有该
名称

看起来答案是在我使用的数据库(SQLite)中,而不是在SQLAlchemy中。SQLite版本您可能还必须使用
关系
工厂定义关系。@sayap使用SQLite,@Keith感谢您的指导,我会给它一个goLooks,就像它可能是SQLite的问题一样。我不知道,但显然sqlite不支持外键约束。。。或者至少没有。我将安装一个新版本,看看会发生什么