Reputation: 2573
I used the flask to create a simple web application. now in model's I have bar table which has the relationship with 2 other table.
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)
now on user_shipper
, i have this:
bars = relationship('bar',backref='shipper',lazy='dynamic')
an also in user_receiver
table :
bars = relationship('bar',backref='receiver',lazy='dynamic')
now I have a problem here:
in bar's table init
function i have this :
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
now when i trying to insert into table with sqlalchemy its show error and say this :
TypeError: __init__() got an unexpected keyword argument 'receiver'
how can I pass the ForeignKey when I trying to insert ? should I define something in init function?
Upvotes: 0
Views: 1240
Reputation: 3825
You need to pass both user_receiver_id
and user_shipper_id
on object initialization.
I have not tested this, but you may try to use this as an insight!
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
Upvotes: 1