Jakub Czaplicki
Jakub Czaplicki

Reputation: 1847

SQLAlchemy+Flask Many to one relationship

I am using Flask + SQLAlchemy + PostgreSQL with two tables. Table with users and a table with goals (what a user want to achieve).

models.py:

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    user_name = Column(Text)
    email = Column(String(255), unique=True, nullable=False)
    password = Column(String(255))
    # A user can only have one goal at a time
    goal_id = Column(Integer, ForeignKey('goals.id'))
    goal = relationship("Goal", back_populates="user")

class Goal(Base):
    """ Goals: Health, Fitness, Wealth, etc. """
    __tablename__ = 'goals'
    id = Column(Integer(), primary_key=True)
    name = Column(String(80), unique=True)
    description = Column(String(255))
    user = relationship("User", back_populates="goal", uselist=False)

A user can only have one goal at a time. In my test case I first populate the table Goal with:

tests.py:

for goal in ["Health", "Fitness", "Wealth"]:
    session.add(Goal(name=goal))

And then I want to be able to populate 3 users each with a different goal. What is the correct way of achieving this ?

Trying (simplified code):

user = User()
goal = Goal()
user.goal.append(goal)

gives me:

  user.goal.append(goal)

E AttributeError: 'NoneType' object has no attribute 'append'

Upvotes: 2

Views: 3848

Answers (1)

pech0rin
pech0rin

Reputation: 5036

You are defining goal as uselist=False which means it is not being stored as an array. You only need to do user.goal = goal instead of append since it is not stored as a list.

So instead of

user = User()
goal = Goal()
user.goal.append(goal)

do this:

user.goal = goal

Upvotes: 1

Related Questions