我用烧瓶创建了一个简单的Web应用程序。 现在在模型中,我有一个条形表,它与另外两个表有关系。
user_shipper_id = Column(Integer, ForeignKey('user_shipper.id', ondelete='CASCADE'), nullable=False)
user_receiver_id = Column(Integer, ForeignKey('user_receiver.id', ondelete='CASCADE'), nullable=False)
现在user_shipper
,我有这个:
bars = relationship('bar',backref='shipper',lazy='dynamic')
也在user_receiver
表中:
bars = relationship('bar',backref='receiver',lazy='dynamic')
现在我有一个问题:
在吧台init
功能我有这个:
def __init__(self, origin, destination, tonage, load_date, unload_date, load_address, unload_adress, unload_type, packing, act_type, truck_type, payment_type):
self.origin = origin
self.destination = destination
self.tonage = tonage
self.load_date = load_date
self.unload_date = unload_date
self.load_adress = load_adress
self.unload_type = unload_type
self.unload_adress = unload_adress
self.packing = packing
self.act_type = act_type
self.truck_type = truck_type
self.payment_type = payment_type
现在当我尝试使用sqlalchemy插入表格时显示错误并说出:
TypeError: __init__() got an unexpected keyword argument 'receiver'
当我尝试插入时如何传递ForeignKey?我应该在init函数中定义一些东西吗?
答案 0 :(得分:1)
您需要在对象初始化时传递user_receiver_id
和user_shipper_id
。
我没有对此进行过测试,但您可以尝试将其用作洞察力!
class UserShipper():
...
bars = relationship('Bar', back_populates='shipper', lazy='dynamic')
class UserReceiver():
...
bars = relationship('Bar', back_populates='receiver', lazy='dynamic')
class Bar():
...
user_shipper_id = Column(Integer, ForeignKey('user_shipper.id', ondelete='CASCADE'), nullable=False)
shipper = relationship('UserShipper', back_populates='bars')
user_receiver_id = Column(Integer, ForeignKey('user_receiver.id', ondelete='CASCADE'), nullable=False)
receiver = relationship('UserReceiver', back_populates='bars')
def __init(self, ..., user_shipper_id, user_receiver_id):
...
self.user_shipper_id = user_shipper_id
self.user_receiver_id = user_receiver_id