Reputation: 5322
I am trying to create the following Structure for my DB:
I have a Parent class UserModel
with ChefModel
and CustomerModel
as children. Everything is working fine until I try adding relations between an OrderModel
and both classes. I get the following error on all calls to UserModel
or ChefModel
. For example, when I try to find a Chef:
File "C:\Users\Moham\Documents\Food order\resources\chef.py", line 60, in get
users = ChefModel.query.filter_by(active = True).all()
AttributeError: 'NoneType' object has no attribute 'filter_by'
Here is my ChefModel:
class ChefModel(UserModel):
__tablename__ = 'chefs'
id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key = True)
food_items = db.relationship("FoodItemModel", backref="chef", lazy=True)
__mapper_args__ = {'polymorphic_identity':'chefs'}
CustomerModel:
class CustomerModel(UserModel):
__tablename__ = 'customers'
id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key = True)
__mapper_args__ = {'polymorphic_identity':'customers'}
And OrderModel:
class OrderModel(db.Model):
__tablename__ = 'orders'
order_id = db.Column(db.Integer, primary_key = True)
updated_date = db.Column(db.DateTime, default=datetime.utcnow())
description = db.Column(db.String(254))
food_items = db.relationship("FoodItemModel", secondary = association_table)
customer_id = db.relationship(db.Integer, db.ForeignKey('customers.id')) //if remove this line
chef_id = db.relationship(db.Integer, db.ForeignKey('chefs.id')) //and this line, calls are normal
What am I doing wrong?
Upvotes: 1
Views: 2357
Reputation: 2563
I think you've incorrectly specified the customer_id and chef_id columns in OrderModel
. Try doing something like this:
class OrderModel(db.Model):
__tablename__ = 'orders'
order_id = db.Column(db.Integer, primary_key = True)
updated_date = db.Column(db.DateTime, default=datetime.utcnow())
description = db.Column(db.String(254))
customer_id = db.Column(db.Integer, db.ForeignKey("customers.id"))
chef_id = db.Column(db.Integer, db.ForeignKey("chefs.id"))
Upvotes: 1