Tuesday 15 April 2014

python - Create many to many on one table -


Flask - SQLLame lets one to create multiple for many relationships, it is made between two different tables is.

Is it possible to create many relationships on the same table? For example, a sister may have many sisters, including many sisters, I have tried: girl_sister_map = db.Table ('girl_sister_map', db.Column ('girl_id' , Db.Integer, db.ForeignKey ('girl.id')), db column ('sister_id', db.Integer, db.ForeignKey ('girl.id')) class girl (db.Model): ID = Db.Column (db.Integer, primary_key = true) name = db.Column (Db.String) sisters = db.relationship ('girl', secondary = girl_sister_map, backref = db.backref ('othersisters', lazy =' Dynamic '))

But when I try to add a girl to a sister:

sqlalchemy.exc.AmbiguousForeignKey SError: Could not determine the status of joining the tables between the parent / child on the relationship Girl.sisters - Specify the tables 'foreign_keys' logic through the secondary table 'girl_sister_map' linking multiple foreign key paths, list those columns Provide a foreign key reference from the secondary table, which should be counted as both parent and child tables.

Is this possible? How am i doing this

You are trying to create what one is called.

This is one in your specific case.

This is supported in SQLLM as you will search by following the previous links. There are many examples in the document.

In order to establish how you would like to be included in the table, you will need the primariesigns and the secondary addicted argument. From the document directly:

  base = declarative_base () node_to_node = table ("node_to_node", Base.metadata, column ("left_node_id", integer, foreignKey ("node.id"), primary_key = True (), column ("right_node_id", integer, foreignKey ("node.id"), primary_key = true)) square node (base): __tablename__ = 'node' id = column (integer, primary_key = true) label = column ( String) right_nodes = relationship ("node", secondary = node_to_node, primaryjoin = id == node_to_node.c.left_node_id, secondaryjoin = id == node_to_node.c.right_node_id, backref = "left_nodes")    

No comments:

Post a Comment