I am working on a small project in which I use Flask-SqlAlchemy to implement an adjacency list relationship. I have a model (table) that has an attribute for db.Relationship() that references the parent_id column of the same table.
Here is the following code (partial):
class Node(db.Model): id = db.Column(db.Integer, primary_key = True) title = db.Column(db.String(80)) folder_id = db.Column(db.Integer, db.ForeignKey('node.id')) children = db.relationship('Node', backref = 'parent', remote_side=[id])
When I try to add a child folder to the parent folder using the backref attribute for the child in the Python shell, for example, the following code (partial):
parentNode = Node('title1') db.session.add(parent) db.session.commit() childNode = Node('title2') child.parent = parentNode
However, I get the following erros after I try this in the Python shell:
Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: Incompatible collection type: Node is not list-like
I read the SqlAlchemy docs to determine the solution, and they have the same syntax for adjacency lists. It has a similar example, but does not show how you can add a parent node to a child node, as I tried in the shell. The reason for adding the parent node element to the property of the child backref is that it works with one-to-many relationships. Any help finding a problem with the code would be greatly appreciated ... Please feel free to suggest alternative solutions.
Thanks!
source share