Thanks a lot This works, I cant remember why I was passing the User object as the argument How can I mark a post as the answer here?
On Monday, September 3, 2018 at 1:29:10 PM UTC+4:30, Simon King wrote: > > Here's a version which I *think* does what you want. There are a > couple of things that you might want to note. > > First, in your paste, Conversation.user1 and Conversation.user2 are > integer columns, but you are assigning User objects to those > properties. That's not the way SQLAlchemy works - you can't assign a > related object directly to the column. In my version below I've > renamed the columns to "userid1" and "userid2" and then created > relationships for "user1" and "user2". > > Second, the User.conversations property needs to have "viewonly=True". > It wouldn't make sense for it to be a writable property, because if > you appended a new Conversation to it, SQLAlchemy wouldn't know which > of the userid1 or userid2 columns to update. > > > #################### > > import sqlalchemy as sa > import sqlalchemy.orm as saorm > from sqlalchemy.ext.declarative import declarative_base > > Base = declarative_base() > > > class Conversation(Base): > __tablename__ = 'conversations' > id = sa.Column(sa.Integer(), primary_key=True) > userid1 = sa.Column( > sa.Integer(), > sa.ForeignKey('users.id'), > nullable=False, > ) > userid2 = sa.Column( > sa.Integer(), > sa.ForeignKey('users.id'), > nullable=False, > ) > > user1 = saorm.relationship( > "User", > primaryjoin="Conversation.userid1 == User.id", > ) > user2 = saorm.relationship( > "User", > primaryjoin="Conversation.userid2 == User.id", > ) > > > class User(Base): > __tablename__ = 'users' > id = sa.Column(sa.Integer(), primary_key=True) > userName = sa.Column(sa.String(32), unique=True, nullable=False) > > conversations = saorm.relationship( > Conversation, > primaryjoin=sa.or_( > id == Conversation.userid1, > id == Conversation.userid2, > ), > viewonly=True, > ) > > > if __name__ == '__main__': > engine = sa.create_engine("sqlite:///:memory:", echo="debug") > Session = saorm.sessionmaker(bind=engine) > Base.metadata.create_all(engine) > > session = Session() > u1 = User(userName="Alireza") > u2 = User(userName="Amir") > session.add_all([u1, u2]) > session.commit() > > con = Conversation(user1=u1, user2=u2) > session.add(con) > session.commit() > > print u1.conversations > > > #################### > On Sun, Sep 2, 2018 at 3:27 PM Alireza Ayin Mehr > <alireza...@gmail.com <javascript:>> wrote: > > > > Well, it seems weird this way, I only have one userId in my parent class > which is the source class > > > > > > On Sunday, September 2, 2018 at 6:08:33 AM UTC+4:30, Seth P wrote: > >> > >> In relationship(), foreign_keys refers to the field in the source > table, not the destination. > > > > -- > > SQLAlchemy - > > The Python SQL Toolkit and Object Relational Mapper > > > > http://www.sqlalchemy.org/ > > > > To post example code, please provide an MCVE: Minimal, Complete, and > Verifiable Example. See http://stackoverflow.com/help/mcve for a full > description. > > --- > > 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 sqlalchemy+...@googlegroups.com <javascript:>. > > To post to this group, send email to sqlal...@googlegroups.com > <javascript:>. > > Visit this group at https://groups.google.com/group/sqlalchemy. > > For more options, visit https://groups.google.com/d/optout. > -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- 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 sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.