smhquery
smhquery

Reputation: 98

many to many relationship flask sqlalchemy

I'm learning Flask and I decided to build a simple market app. I have two types of users, sellers and buyers. I have created a user class which has two children (buyer, seller). sellers should have this ability to upload some products in order to sell them. Therefore I should create a class which is a child of seller. I think since the relation between user and buyer is on to many, the relation between the product and seller should be many to many.

If I've done something right up until now, how should I create this new table.

Upvotes: 0

Views: 155

Answers (1)

AcDc
AcDc

Reputation: 26

It would be more beneficial to create product database in a way that do not inherit from seller. and that relate the user database to product through another table, something like this:

class User(db.Model):
    id = db.Column(db.Integer(), primary_key=True)
    ...
class Product(db.Model):
    id = db.Column(db.Integer(), primary_key=True)
    ...
class Order(db.Model):
    id = db.Column(db.Integer(), primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    product_id = db.Column(db.Integer, db.ForeignKey('product.id'))
    user = db.relationship('User', backref=db.backref("order", cascade="all, delete-orphan"))
    product = db.relationship('Product', backref=db.backref("order", cascade="all, delete-orphan"))





Upvotes: 1

Related Questions