Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7585871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:16:55+00:00 2026-05-30T19:16:55+00:00

I have this to SQLAlchemy(using the Flask SqlAlchemy) objects defined: class User(db.Model): id =

  • 0

I have this to SQLAlchemy(using the Flask SqlAlchemy) objects defined:

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True)
    password = db.Column(db.String(30))
    email = db.Column(db.String(45), unique=True)
    friends = db.relationship('Friend', backref='user',
                                lazy='dynamic')

    def __init__(self, username, password, email):
        self.username = username
        self.password = password
        self.email = email

    def __repr__(self):
        return "<User('%s','%s','%s')>" % (self.username, self.email, self.id)


class Friend(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    userId = db.Column(db.Integer, db.ForeignKey('user.id'))
    friendId = db.Column(db.Integer)
    created = db.Column(db.DateTime)

    def __init__(self, userId, friendId):
        self.userId = userId
        self.friendId = friendId
        self.created = datetime.datetime.now()

    def __repr__(self):
        return "<Friend(%i,%i)>" % (self.userId, self.friendId)

As I understand to add a friend I should be able to do something like this:

First getting the user:

MyUser = bpdata.User.query.filter_by(id=1).first()

Getting the friend:

MyFriend = bpdata.User.query.filter_by(id=2).first()

Now I would like to do:

MyUser.Friends.Append(MyFriend)

Is this possible or do I just have to add the friend IDs directly into the table Friend?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:16:56+00:00Added an answer on May 30, 2026 at 7:16 pm

    Figured this one out my self..

    What I needed to do was:

    MyUser.friends.append(Friend(MyUser.id, MyFriend.id))
    

    and then commit the update.

    Update:

    Ok I found the proper way of doing what I wanted. First I don’t need the Friend table/class at all. Full code:

    association_table = db.Table('association',
        db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
        db.Column('friend_id', db.Integer, db.ForeignKey('user.id'))
    )
    class User(db.Model):
        id = db.Column(db.Integer, primary_key=True)
        username = db.Column(db.String(20), unique=True)
        password = db.Column(db.String(30))
        email = db.Column(db.String(45), unique=True)
        friends = db.relationship("User",
                    secondary=association_table,
                    backref='added_by',
                    primaryjoin=id == association_table.c.user_id,
                    secondaryjoin=id == association_table.c.friend_id)
    

    With this I can now do following:

    >>> user1 = User.query.filter_by(id=1).first()
    >>> user1.friends
    []
    >>> user2 = User.query.filter_by(id=2).first()
    >>> user1.friends.append(user2)
    >>> user1.friends
    [<User('user1','user1@admin.com','2')>]
    >>> user1.friends[0].added_by
    [<User('admin','admin@admin.com','1')>]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using __init__() like this in some SQLAlchemy ORM classes that have many parameters
I have a Test model/table and a TestAuditLog model/table, using SQLAlchemy and SQL Server
I have a Flask web application using SQLAlchemy with MySQL, and I have set
I have this string 'john smith~123 Street~Apt 4~New York~NY~12345' Using JavaScript, what is the
I have a Flask, SQLAlchemy webapp which uses a single mysql server. I want
Is it possible to have the postgres database dump(pg_dump) using SQLAlchemy? i can get
I'm using Flask framework with sqlalchemy and I can't query some related table of
I just started with Flask and SQLAlchemy in flask. So I have a many-to-many
We're using SQLAlchemy declarative base and I have a method that I want isolate
I access a a postgres table using SQLAlchemy. I want a query to have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.