I have a three classes that I am using to represent a tree:
Tree has some information about the tree at large and a list of nodes.
Node has some information specific to a node, a list of subtrees, and needs
a particular piece of information about the Tree for a constraint.
SubTree has a list of nodes.
Here is some (working) example code for this setup:
from sqlalchemy import (
Column, Integer, UniqueConstraint, ForeignKeyConstraint,
create_engine
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
Base = declarative_base()
class Tree(Base):
__tablename__ = 'tree'
id = Column(Integer, primary_key=True)
important_value = Column(Integer, nullable=False)
nodes = relationship('Node')
class SubTree(Base):
__tablename__ = 'sub_tree'
id = Column(Integer, primary_key=True)
parent_node_id = Column(Integer, nullable=False)
important_value = Column(Integer, nullable=False)
nodes = relationship(
'Node',
foreign_keys='[Node.parent_sub_tree_id, Node.important_value]',
)
__table_args__ = (
UniqueConstraint('id', 'important_value'),
ForeignKeyConstraint(
['parent_node_id', 'important_value'],
['node.id', 'node.important_value']
),
)
class Node(Base):
__tablename__ = 'node'
id = Column(Integer, primary_key=True)
parent_tree_id = Column(Integer)
important_value = Column(Integer, nullable=False)
parent_sub_tree_id = Column(Integer)
sub_trees = relationship(
'SubTree',
foreign_keys=(
'[SubTree.parent_node_id, SubTree.important_value]'
),
)
__table_args__ = (
UniqueConstraint('id', 'important_value'),
ForeignKeyConstraint(
['parent_tree_id', 'important_value'],
['tree.id', 'tree.important_value']
),
ForeignKeyConstraint(
['parent_sub_tree_id', 'important_value'],
['sub_tree.id', 'sub_tree.important_value']
),
)
engine = create_engine('...')
Base.metadata.create_all(engine)
session = sessionmaker(bind=engine, autocommit=True)()
with session.begin():
session.add(
Tree(
important_value=3,
nodes=[
Node(
sub_trees=[
SubTree(
nodes=[
Node(),
],
),
],
),
],
)
)
When I run this example, I get this warning:
/.../lib/python3.4/site-packages/sqlalchemy/orm/relationships.py:2667:
SAWarning: relationship 'Tree.nodes' will copy column tree.important_value
to column node.important_value, which conflicts with relationship(s):
'SubTree.nodes' (copies sub_tree.important_value to node.important_value).
Consider applying viewonly=True to read-only relationships, or provide a
primaryjoin condition marking writable columns with the foreign() annotation
.
for (pr, fr_) in other_props)
I can't figure out how to modify the relationships to make SQLAlchemy happy
with my (admittedly convoluted) scheme.
Any ideas?
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.