Sqlalchemy User Product Reviews Relation model deisgn
<p>Need models so that a User can have a Products and Users can leave Reviews on Products made by other users. I was thinking having a one to many relationship from products to reviews but then how do which users left which review. This is what i have so far. </p>

<pre><code>class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(255), unique=True)
    products =  db.relationship('Product', backref='products',
                                lazy='dynamic')


class Review(db.Model):
    id = db.Column(db.Integer(), primary_key=True)
    stars =  db.Column(db.String(255))
    description =  db.Column(db.String(255))


class Product(db.Model):
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    id = db.Column(db.Integer(), primary_key=True)
</code></pre>

<p>You just need to add foreign keys for <code>User</code> and <code>Product</code> into your <code>Review</code> table:</p>

<pre><code>class Review(db.Model):
    # ... 
    product_id = db.Column(db.Integer, db.ForeignKey('product.id'))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
</code></pre>

<p>You can also then add backrefs for <code>Reviews</code> on to your <code>User</code> and <code>Product</code>, and you can filter the <code>user.reviews</code> by <code>product_id</code>. </p>

python sql flask sqlalchemy flask-sqlalchemy