Пытаюсь вставить запись, ссылающуюся на две родительские:
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy import ForeignKey
Base = declarative_base()
engine = create_engine('sqlite://')
Session = sessionmaker(engine)
class Parent(Base):
__tablename__ = 'Parent'
link = Column('link', Integer, primary_key=True, autoincrement=True)
name = Column('c_name', String)
child = relationship('Child')
class Parent2(Base):
__tablename__ = 'Parent2'
link = Column('link', Integer, primary_key=True, autoincrement=True)
name = Column('c_name', String)
child = relationship('Child')
class Child(Base):
__tablename__ = 'Child'
link = Column('link', Integer, primary_key=True, autoincrement=True)
parent = Column('f_parent', Integer, ForeignKey('Parent.link'), nullable=False)
parent2 = Column('f_parent2', Integer, ForeignKey('Parent2.link'), nullable=False)
name = Column('c_name', String)
Base.metadata.create_all(engine)
session = Session()
parent = Parent(name='qwe')
parent2 = Parent2(name='qwerty')
session.add(parent)
session.add(parent2)
session.commit()
child = Child(name='child')
parent.child.append(child)
parent2.child.append(child)
session.add_all([parent, parent2])
session.commit()
Получаю ошибку:
(sqlite3.IntegrityError) NOT NULL constraint failed: Child.f_parent2 [SQL: INSERT INTO «Child» (f_parent, f_parent2, c_name) VALUES (?, ?, ?)] [parameters: (1, None, 'child')]
Как правильно вставить такую запись?