Querying user specific data in flask

Viewed 11

I'm building an app that shows a database of different plant species. Each user sees the same table of plant species except for the "notes" column, which each user can edit to their own liking. This is the database structure I have created:

class Note(db.Model):
    __tablename__ = 'plantdatabasenote'
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.Text(), nullable=False)
    plant_id = db.Column(db.Integer, db.ForeignKey("plant.id"))
    user_id = db.Column(db.Integer, db.ForeignKey("profile.id"))

class Plant(db.Model):
    __tablename__ = 'plant'
    id = db.Column(db.Integer, primary_key=True)
    common_name = db.Column(db.String(200), nullable=False)
    date_created = db.Column(db.DateTime, default=datetime.utcnow)
    notes = db.relationship("Note", backref="user_notes")

class Profile(db.Model):
    __tablename__ = 'profile'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(200), unique=True, nullable=False)

I tried to retrieve the notes of user 1 with the following:

Plant.query.filter(Plant.notes.any(user_id=1)).all()

Unfortunately, this does not give me all the plants with the notes of user 1. Any idea how to fix this?

0 Answers
Related