SQLAlchemy User Many to Many Relationship

Adnan Dossaji

I am working on a user structure where a user can have parents and children that consist of user objects. I have been trying to get the following to work in multiple different ways in SQLAlchemy and Flask.

Here is an example of how I want to structure this:

UserTable
id | name
---+-----
1  | Kim
2  | Tammy
3  | John
4  | Casey
5  | Kyle

UserRelationship
id | parent_user_id | child_user_id
---+----------------+---------------
1  | 1              | 2
2  | 1              | 3
3  | 4              | 2

Where Kim is the parent to Tammy and John. Casey is the parent of Tammy. Tammy is the child of Kim and Casey. John is the child of Kim. Kyle has no children nor parents.


My error reads:

sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join condition 
between parent/child tables on relationship User.parents - there are multiple 
foreign key paths linking the tables via secondary table 'user_relationship'. 
Specify the 'foreign_keys' argument, providing a list of those columns which 
should be counted as containing a foreign key reference from the secondary 
table to each of the parent and child tables.

My model.py file looks like:

user_relationship = db.Table(
    'user_relationship',
    db.Model.metadata,
    db.Column('child_user_id', db.Integer, db.ForeignKey('user.id')),
    db.Column('parent_user_id', db.Integer, db.ForeignKey('user.id'))
)

class User(db.Model):
    __tablename__ = 'user'

    id = db.Column(db.Integer, primary_key=True, unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True)
    pwdhash = db.Column(db.String(54))

    parents = relationship('User',
        secondary=user_relationship,
        backref=db.backref('children'),
        cascade="all,delete")

This might not be the best way to handle many to many hierarchical user relationships in Flask or SQLAlchemy. Any insight on how to build this out would be great.

Thank you

Shahryar Saljoughi

db.Table is mainly used when there is a Many to Many relation between two different entities. Here both parent and child are Users.

In your case, below code would be fine:

class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True, unique=True,     nullable=False)
    email = db.Column(db.String(120), unique=True)
    pwdhash = db.Column(db.String(54))


class Parent(db.Model):
    __tablename__ = 'parents'
    child_id = db.Column(db.Integer, db.Foreignkey('users.id'))
    parent_id = db.Column(db.Integer, db.Foreignkey('users.id'))
    child = db.relationship('User', foreign_keys=[child_id], backref = 'parents')

flask_sqlalchemy is written on top of SQLAlchemy, and there is a nice elaboration of this issue here (Handling Multiple Join Paths) in SQLAlchemy's website.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sqlalchemy many to many relationship

From Dev

SQLAlchemy Many to Many relationship parameter

From Dev

SQLAlchemy: filter on operator in a many-to-many relationship

From Dev

SQLAlchemy. How to order on many to many relationship?

From Dev

SQLAlchemy: filter on operator in a many-to-many relationship

From Dev

SQLAlchemy. How to order on many to many relationship?

From Dev

SQLAlchemy: Pass information to automap many to many relationship

From Dev

SQLAlchemy Many To Many relationship returning duplicate children

From Dev

Sqlalchemy one to many relationship join?

From Dev

Query One to Many Relationship SQLAlchemy

From Dev

One to many + one relationship in SQLAlchemy?

From Dev

Query One to Many Relationship SQLAlchemy

From Dev

How to create relationship many to many in SQLAlchemy (python, flask) for model User to itself

From Dev

How to create relationship many to many in SQLAlchemy (python, flask) for model User to itself

From Dev

Laravel User - Role many to many relationship

From Dev

SQLAlchemy one to many relationship, how to filter the collection

From Dev

SQLAlchemy+Flask Many to one relationship

From Dev

SQLAlchemy one to many relationship, how to filter the collection

From Dev

Creating One-to-Many with Relationship for the ORM in SQLAlchemy

From Dev

Flask-SQLAlchemy: Update a One to Many Relationship

From Dev

SQLAlchemy - Self referential Many-to-many relationship with extra column

From Dev

How do 'primaryjoin' and 'secondaryjoin' work for many-to-many relationship in SQLAlchemy?

From Dev

Flask-Sqlalchemy 3 way many to many relationship

From Dev

How to order by count of many-to-many relationship in SQLAlchemy?

From Dev

SQLAlchemy Many-to-Many Relationship on a Single Table Error: NoReferencedTableError

From Dev

SQLAlchemy Attempting to Twice Delete Many to Many Secondary Relationship

From Dev

SQLAlchemy Inserting Data in a Many-to-Many Relationship with Association Table

From Dev

Should I use flask sqlalchemy many-to-many relationship?

From Dev

SQLAlchemy: filtering count in many-to-many relationship query

Related Related

  1. 1

    Sqlalchemy many to many relationship

  2. 2

    SQLAlchemy Many to Many relationship parameter

  3. 3

    SQLAlchemy: filter on operator in a many-to-many relationship

  4. 4

    SQLAlchemy. How to order on many to many relationship?

  5. 5

    SQLAlchemy: filter on operator in a many-to-many relationship

  6. 6

    SQLAlchemy. How to order on many to many relationship?

  7. 7

    SQLAlchemy: Pass information to automap many to many relationship

  8. 8

    SQLAlchemy Many To Many relationship returning duplicate children

  9. 9

    Sqlalchemy one to many relationship join?

  10. 10

    Query One to Many Relationship SQLAlchemy

  11. 11

    One to many + one relationship in SQLAlchemy?

  12. 12

    Query One to Many Relationship SQLAlchemy

  13. 13

    How to create relationship many to many in SQLAlchemy (python, flask) for model User to itself

  14. 14

    How to create relationship many to many in SQLAlchemy (python, flask) for model User to itself

  15. 15

    Laravel User - Role many to many relationship

  16. 16

    SQLAlchemy one to many relationship, how to filter the collection

  17. 17

    SQLAlchemy+Flask Many to one relationship

  18. 18

    SQLAlchemy one to many relationship, how to filter the collection

  19. 19

    Creating One-to-Many with Relationship for the ORM in SQLAlchemy

  20. 20

    Flask-SQLAlchemy: Update a One to Many Relationship

  21. 21

    SQLAlchemy - Self referential Many-to-many relationship with extra column

  22. 22

    How do 'primaryjoin' and 'secondaryjoin' work for many-to-many relationship in SQLAlchemy?

  23. 23

    Flask-Sqlalchemy 3 way many to many relationship

  24. 24

    How to order by count of many-to-many relationship in SQLAlchemy?

  25. 25

    SQLAlchemy Many-to-Many Relationship on a Single Table Error: NoReferencedTableError

  26. 26

    SQLAlchemy Attempting to Twice Delete Many to Many Secondary Relationship

  27. 27

    SQLAlchemy Inserting Data in a Many-to-Many Relationship with Association Table

  28. 28

    Should I use flask sqlalchemy many-to-many relationship?

  29. 29

    SQLAlchemy: filtering count in many-to-many relationship query

HotTag

Archive