简体   繁体   中英

Filed in SQLAlchemy of user's list

I have one model that is Users in which there is a field in this model that I would like to store a list of Users. The idea is that you can add frieds and store them somewhere.

class User (db.Model):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True)
    username = Column(String(50), unique = True)
    email = Column(String(120), unique = True)
    password = Column(String(50))
    date = Column(DateTime(), default=datetime.now())
    friends = "Should be a list of users"

I have thought to have a string with the id of each user but, is there any posibility to do it with a relationship to the same model? like this:

friends = relationship("User")

Thanks a lot!

Proposed solutions based on Adjacency List Relationships would only work in case when someone can be a friend of maximum one person, which I do not believe to be the case in the real world.

A pattern you need to apply in this case is called Self-Referential Many-to-Many Relationship . Please read the sample linked to above. In order to make it work for your model, you would need to create additional table to keep the pairs of friends, and configure the relationship as below:

# object model
t_userfriend = Table("user_friend", Base.metadata,
    Column("user_id", Integer, ForeignKey("users.id"), primary_key = True),
    Column("friend_id", Integer, ForeignKey("users.id"), primary_key = True),
)

class User (Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True)
    name = Column(String(50), unique = True)
    # ...
    friends = relationship("User",
                          secondary = t_userfriend,
                          primaryjoin = (id == t_userfriend.c.user_id),
                          secondaryjoin = (id == t_userfriend.c.friend_id),
                          backref = "friend_of",
                          )

I guess that the other question you need to ask yourself is whether in your model if A is a friend of B, does this mean that B is a friend of A? In case this is true, you might want/need to:

  1. either store just one side of the relationship, and calculate the other
  2. make sure you always store both sides to the relationship

You can use Adjacency List Relationship and this link have the same issue so you can learn from it. How to create relationship many to many in SQLAlchemy (python, flask) for model User to itself

Yes you can do it with Adjacency List Relationships .

class User (db.Model):

    __tablename__ = "users"

    id = Column(Integer, primary_key=True)
    username = Column(String(50), unique = True)
    email = Column(String(120), unique = True)
    password = Column(String(50))
    date = Column(DateTime(), default=datetime.now())
    friends = relationship("User",
            backref=backref('parent', remote_side=[id])
        )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM