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>I'm imagining setting it up like this (with the new one-to-many included as well - I think I've got that right).  You should know the product's ID at the time you're creating the entry in the Python code, so you can simply add it in.  I don't think you would necessarily need to create a relationship for that.</p>

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


class Review(db.Model):
    id = db.Column(db.Integer(), primary_key=True)
    product_id = db.Column(db.Integer, db.ForeignKey('product.id'))
    stars =  db.Column(db.String(255))
    description =  db.Column(db.String(255))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))


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>I changed a line in your <code>User</code> class.  You had</p>

<pre><code>products =  db.relationship('Product', backref='product',
                            lazy='dynamic')
</code></pre>

<p>But I think it's supposed to be</p>

<pre><code>products =  db.relationship('Product', backref='user',
                            lazy='dynamic')
</code></pre>

python sql flask sqlalchemy flask-sqlalchemy