Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.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_Sql_Postgresql_Sqlalchemy - Fatal编程技术网

Python SQLAlchemy一对一关系创建多行

Python SQLAlchemy一对一关系创建多行,python,sql,postgresql,sqlalchemy,Python,Sql,Postgresql,Sqlalchemy,我试图在我的数据库(postgresql)中的两个表之间建立一对一的关系。我正在python中使用SQLAlchemy。因此,我使用了文档中给出的一个示例 这将创建两个表parent和child。 现在我在父表和子表中插入值 from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from test_databasesetup import Base, Parent, Child engine =

我试图在我的数据库(postgresql)中的两个表之间建立一对一的关系。我正在python中使用SQLAlchemy。因此,我使用了文档中给出的一个示例

这将创建两个表parent和child。 现在我在父表和子表中插入值

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from test_databasesetup import Base, Parent, Child

engine = create_engine('postgresql+psycopg2://testdb:hello@localhost/fullstack')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()

parent = Parent()
session.add(parent)
session.commit() // insert in 'parent' table with id=1
// insert into child
child = Child(parent_id=1)
session.add(child)
session.commit()

child = Child(parent_id=1)
session.add(child)
session.commit() 
再次插入具有相同父\u id的子项本应引发错误,但已插入记录

id | parent_id 
----+-----------
  1 |         1
  2 |         1
这里应该做些什么,以便我只能插入一个与父id对应的子项。我不希望子项具有相同的父id


谢谢。

问题是您直接指定了字段
parent\u id
。在这种情况下,
sqlalchemy
没有机会验证一对一的关系。相反,请使用以下关系:

# add new parent to the database
parent = Parent()
session.add(parent)
session.commit()

# insert new child for this parent
child = Child(parent=parent)  # @note: this is the code change. \
# Here we refer to parent, not parent_id field
session.add(child)
session.commit()

# insert another child for the same parent:
# this will assign this new child to the parent, and will 
# *remove* previous child from this parent
child = Child(parent=parent)
session.add(child)
session.commit()
另一个副作用是代码更加简洁。还有一个事实是
sqlalchemy
可以识别外键本身,您不需要知道对象的
id

parent = Parent()
child = Child(parent=parent)
# it is enough to add only one related object, and the other (Child) will be added to session automatically
session.add(parent)
# we can commit only now, when we finished working on all the objects in the current session/transaction
session.commit()

此外,您可以将唯一约束添加到
Child.parent\u id
字段,作为附加的数据库级检查,这将在您的情况下引发错误:

parent = Parent()
child = Child(parent=parent)
# it is enough to add only one related object, and the other (Child) will be added to session automatically
session.add(parent)
# we can commit only now, when we finished working on all the objects in the current session/transaction
session.commit()
parent_id = Column(Integer, ForeignKey('parent.id'), unique=True)