SQLAlchemy problem with relationship many to many









up vote
0
down vote

favorite












I have a issue using SQLAlchemy.



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)
)

class 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"
)

metadata = Base.metadata

engine = create_engine("postgres+psycopg2://postgres:admin@localhost:5432/test")
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)

session = sessionmaker()
session.configure(bind=engine)

s = session()

node1 = Node(label="node1")
node2 = Node(label="node2")
node3 = Node(label="node3")
node1.right_nodes.append(node2)
node1.right_nodes.append(node3)
s.add_all([node1, node2, node3])
s.commit()


what I want is to have the IDs of all the right nodes of node 1 as an array, so here [2, 3]
Do you know a method that allows me to do that?



Thanks in advance for any help!



EDIT :



class NeedTag(Base):
__tablename__ = 'need_tags'
tag_id = Column(Integer, ForeignKey('tags.id'), primary_key=True)
text_id = Column(Integer, ForeignKey('texts.id'), primary_key=True)

class Text(Base) :
__tablename__ = 'texts'

id = Column(Integer, primary_key=True)
text = Column(Text, nullable=False)

need_tag = relationship("Tag",
secondary="need_tags")

class Tag(Base):
__tablename__ = 'tags'

id = Column(Integer, primary_key=True)
name = Column(String(), nullable=False)


I have a problem with the SQL Alchemy synthax.
For example, I defined the Text and Tags class and a many-to-many relationship between Text and Tags: To use a text, you need to have some tags that must be present.



An example of a condition is:
If tag1 or tag2 :
If tag3 or tag 4 :
Text1 can be used



I would like to do this:



text1.need_tags = [ [tag1, tag2],[tag3, tag4] ] 


And therefore to use text1, you need at least 1 tag in the 1st list AND 1 tag in the 2nd list



But I tried so many techniques and they all failed



Do you know if it is possible to do that with SQLAlchemy ? Or something like that that accomplish the same thing










share|improve this question























  • [node.id for node in node1.right_nodes]?
    – SuperShoot
    Nov 10 at 10:29










  • I was also wondering: is it possible to add at the same time node2 and node3 to node1's right nodes in one line of code ? Instead of using node1.right_nodes.append(node2), node1.right_nodes.append(node3), is there any method which acts like node1.right_nodes.append([node2, node3]) (this one does not work) ?
    – AlbertTho
    Nov 10 at 10:51











  • Relationship attributes are just lists by default. So if you are just initially setting the relationship node1.right_nodes = [node2, node3]. If you want to add to an existing relationship collection, node1.right_nodes += [node2, node3].
    – SuperShoot
    Nov 10 at 21:39










  • Hi @AlbertTho, you should really ask a new question rather than edit an old question to include a new problem.
    – SuperShoot
    Nov 13 at 10:28














up vote
0
down vote

favorite












I have a issue using SQLAlchemy.



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)
)

class 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"
)

metadata = Base.metadata

engine = create_engine("postgres+psycopg2://postgres:admin@localhost:5432/test")
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)

session = sessionmaker()
session.configure(bind=engine)

s = session()

node1 = Node(label="node1")
node2 = Node(label="node2")
node3 = Node(label="node3")
node1.right_nodes.append(node2)
node1.right_nodes.append(node3)
s.add_all([node1, node2, node3])
s.commit()


what I want is to have the IDs of all the right nodes of node 1 as an array, so here [2, 3]
Do you know a method that allows me to do that?



Thanks in advance for any help!



EDIT :



class NeedTag(Base):
__tablename__ = 'need_tags'
tag_id = Column(Integer, ForeignKey('tags.id'), primary_key=True)
text_id = Column(Integer, ForeignKey('texts.id'), primary_key=True)

class Text(Base) :
__tablename__ = 'texts'

id = Column(Integer, primary_key=True)
text = Column(Text, nullable=False)

need_tag = relationship("Tag",
secondary="need_tags")

class Tag(Base):
__tablename__ = 'tags'

id = Column(Integer, primary_key=True)
name = Column(String(), nullable=False)


I have a problem with the SQL Alchemy synthax.
For example, I defined the Text and Tags class and a many-to-many relationship between Text and Tags: To use a text, you need to have some tags that must be present.



An example of a condition is:
If tag1 or tag2 :
If tag3 or tag 4 :
Text1 can be used



I would like to do this:



text1.need_tags = [ [tag1, tag2],[tag3, tag4] ] 


And therefore to use text1, you need at least 1 tag in the 1st list AND 1 tag in the 2nd list



But I tried so many techniques and they all failed



Do you know if it is possible to do that with SQLAlchemy ? Or something like that that accomplish the same thing










share|improve this question























  • [node.id for node in node1.right_nodes]?
    – SuperShoot
    Nov 10 at 10:29










  • I was also wondering: is it possible to add at the same time node2 and node3 to node1's right nodes in one line of code ? Instead of using node1.right_nodes.append(node2), node1.right_nodes.append(node3), is there any method which acts like node1.right_nodes.append([node2, node3]) (this one does not work) ?
    – AlbertTho
    Nov 10 at 10:51











  • Relationship attributes are just lists by default. So if you are just initially setting the relationship node1.right_nodes = [node2, node3]. If you want to add to an existing relationship collection, node1.right_nodes += [node2, node3].
    – SuperShoot
    Nov 10 at 21:39










  • Hi @AlbertTho, you should really ask a new question rather than edit an old question to include a new problem.
    – SuperShoot
    Nov 13 at 10:28












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a issue using SQLAlchemy.



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)
)

class 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"
)

metadata = Base.metadata

engine = create_engine("postgres+psycopg2://postgres:admin@localhost:5432/test")
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)

session = sessionmaker()
session.configure(bind=engine)

s = session()

node1 = Node(label="node1")
node2 = Node(label="node2")
node3 = Node(label="node3")
node1.right_nodes.append(node2)
node1.right_nodes.append(node3)
s.add_all([node1, node2, node3])
s.commit()


what I want is to have the IDs of all the right nodes of node 1 as an array, so here [2, 3]
Do you know a method that allows me to do that?



Thanks in advance for any help!



EDIT :



class NeedTag(Base):
__tablename__ = 'need_tags'
tag_id = Column(Integer, ForeignKey('tags.id'), primary_key=True)
text_id = Column(Integer, ForeignKey('texts.id'), primary_key=True)

class Text(Base) :
__tablename__ = 'texts'

id = Column(Integer, primary_key=True)
text = Column(Text, nullable=False)

need_tag = relationship("Tag",
secondary="need_tags")

class Tag(Base):
__tablename__ = 'tags'

id = Column(Integer, primary_key=True)
name = Column(String(), nullable=False)


I have a problem with the SQL Alchemy synthax.
For example, I defined the Text and Tags class and a many-to-many relationship between Text and Tags: To use a text, you need to have some tags that must be present.



An example of a condition is:
If tag1 or tag2 :
If tag3 or tag 4 :
Text1 can be used



I would like to do this:



text1.need_tags = [ [tag1, tag2],[tag3, tag4] ] 


And therefore to use text1, you need at least 1 tag in the 1st list AND 1 tag in the 2nd list



But I tried so many techniques and they all failed



Do you know if it is possible to do that with SQLAlchemy ? Or something like that that accomplish the same thing










share|improve this question















I have a issue using SQLAlchemy.



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)
)

class 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"
)

metadata = Base.metadata

engine = create_engine("postgres+psycopg2://postgres:admin@localhost:5432/test")
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)

session = sessionmaker()
session.configure(bind=engine)

s = session()

node1 = Node(label="node1")
node2 = Node(label="node2")
node3 = Node(label="node3")
node1.right_nodes.append(node2)
node1.right_nodes.append(node3)
s.add_all([node1, node2, node3])
s.commit()


what I want is to have the IDs of all the right nodes of node 1 as an array, so here [2, 3]
Do you know a method that allows me to do that?



Thanks in advance for any help!



EDIT :



class NeedTag(Base):
__tablename__ = 'need_tags'
tag_id = Column(Integer, ForeignKey('tags.id'), primary_key=True)
text_id = Column(Integer, ForeignKey('texts.id'), primary_key=True)

class Text(Base) :
__tablename__ = 'texts'

id = Column(Integer, primary_key=True)
text = Column(Text, nullable=False)

need_tag = relationship("Tag",
secondary="need_tags")

class Tag(Base):
__tablename__ = 'tags'

id = Column(Integer, primary_key=True)
name = Column(String(), nullable=False)


I have a problem with the SQL Alchemy synthax.
For example, I defined the Text and Tags class and a many-to-many relationship between Text and Tags: To use a text, you need to have some tags that must be present.



An example of a condition is:
If tag1 or tag2 :
If tag3 or tag 4 :
Text1 can be used



I would like to do this:



text1.need_tags = [ [tag1, tag2],[tag3, tag4] ] 


And therefore to use text1, you need at least 1 tag in the 1st list AND 1 tag in the 2nd list



But I tried so many techniques and they all failed



Do you know if it is possible to do that with SQLAlchemy ? Or something like that that accomplish the same thing







python sql postgresql orm sqlalchemy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 16:02

























asked Nov 9 at 23:52









AlbertTho

14




14











  • [node.id for node in node1.right_nodes]?
    – SuperShoot
    Nov 10 at 10:29










  • I was also wondering: is it possible to add at the same time node2 and node3 to node1's right nodes in one line of code ? Instead of using node1.right_nodes.append(node2), node1.right_nodes.append(node3), is there any method which acts like node1.right_nodes.append([node2, node3]) (this one does not work) ?
    – AlbertTho
    Nov 10 at 10:51











  • Relationship attributes are just lists by default. So if you are just initially setting the relationship node1.right_nodes = [node2, node3]. If you want to add to an existing relationship collection, node1.right_nodes += [node2, node3].
    – SuperShoot
    Nov 10 at 21:39










  • Hi @AlbertTho, you should really ask a new question rather than edit an old question to include a new problem.
    – SuperShoot
    Nov 13 at 10:28
















  • [node.id for node in node1.right_nodes]?
    – SuperShoot
    Nov 10 at 10:29










  • I was also wondering: is it possible to add at the same time node2 and node3 to node1's right nodes in one line of code ? Instead of using node1.right_nodes.append(node2), node1.right_nodes.append(node3), is there any method which acts like node1.right_nodes.append([node2, node3]) (this one does not work) ?
    – AlbertTho
    Nov 10 at 10:51











  • Relationship attributes are just lists by default. So if you are just initially setting the relationship node1.right_nodes = [node2, node3]. If you want to add to an existing relationship collection, node1.right_nodes += [node2, node3].
    – SuperShoot
    Nov 10 at 21:39










  • Hi @AlbertTho, you should really ask a new question rather than edit an old question to include a new problem.
    – SuperShoot
    Nov 13 at 10:28















[node.id for node in node1.right_nodes]?
– SuperShoot
Nov 10 at 10:29




[node.id for node in node1.right_nodes]?
– SuperShoot
Nov 10 at 10:29












I was also wondering: is it possible to add at the same time node2 and node3 to node1's right nodes in one line of code ? Instead of using node1.right_nodes.append(node2), node1.right_nodes.append(node3), is there any method which acts like node1.right_nodes.append([node2, node3]) (this one does not work) ?
– AlbertTho
Nov 10 at 10:51





I was also wondering: is it possible to add at the same time node2 and node3 to node1's right nodes in one line of code ? Instead of using node1.right_nodes.append(node2), node1.right_nodes.append(node3), is there any method which acts like node1.right_nodes.append([node2, node3]) (this one does not work) ?
– AlbertTho
Nov 10 at 10:51













Relationship attributes are just lists by default. So if you are just initially setting the relationship node1.right_nodes = [node2, node3]. If you want to add to an existing relationship collection, node1.right_nodes += [node2, node3].
– SuperShoot
Nov 10 at 21:39




Relationship attributes are just lists by default. So if you are just initially setting the relationship node1.right_nodes = [node2, node3]. If you want to add to an existing relationship collection, node1.right_nodes += [node2, node3].
– SuperShoot
Nov 10 at 21:39












Hi @AlbertTho, you should really ask a new question rather than edit an old question to include a new problem.
– SuperShoot
Nov 13 at 10:28




Hi @AlbertTho, you should really ask a new question rather than edit an old question to include a new problem.
– SuperShoot
Nov 13 at 10:28

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234726%2fsqlalchemy-problem-with-relationship-many-to-many%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234726%2fsqlalchemy-problem-with-relationship-many-to-many%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo