Re: [sqlalchemy] composite secondary join variant

2020-04-24 Thread Jonathan Vanasco
Thanks! That works exactly as I needed.  I knew there was a problem in the 
secondaryjoin, so i commented it out.

This works more intuitively than my other composite relationships, which 
are all more complex. The joining you used is:

primaryjoin: A->B
secondaryjoin: B->B2C
secondary: B2C->C

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/71b757c9-6665-4cb2-8b57-d8f7fddd9fe5%40googlegroups.com.


Re: [sqlalchemy] composite secondary join variant

2020-04-24 Thread Elmer de Looff
Hi Jonathan,

>From toying with it a little bit, it looks like you *need* to specify a
secondaryjoin when you specify the secondary table. In your example, the
secondary does some of the work that the secondaryjoin would need to do.
I've created a gist that mirrors your table setup (with some more elaborate
naming) that hopefully provides you with what you're looking for.

https://gist.github.com/edelooff/c1bb7f7912e49d01677fcce0003663fe

On Fri, Apr 24, 2020 at 2:01 AM Jonathan Vanasco  wrote:

> i'm stuck on a variant of the Composite Secondary Join (
> https://docs.sqlalchemy.org/en/13/orm/join_conditions.html#composite-secondary-joins
> )
>
> I hope someone can see what my tired eyes are missing. I'm fairly certain
> the issue is in `secondary' and 'secondaryjoin'.  I've tried a handful of
> variants I thought would work, but trigger this error:
>
> sqlalchemy.exc.InvalidRequestError: Class  does not
> have a mapped column named 'get_children'
>
> I've used this pattern with much luck in the past: `primaryjoin` goes from
> the base class to whatever I build the secondary/secondary join from.
>
> I've distilled the relationship pattern as below:
>
> * `A` does not fkey onto anything.
> * `B` fkeys onto `A`
> * The intended relationship on `A` is a list of `C` items, which are
> related to `B` through an association table
>
> I can build out a the relationship from B, and I could probably mount it
> onto A with an association_proxy, but I'd like to understand what I'm doing
> wrong with the `relationship` off A.  This is puzzling me.
>
> Thanks in advace.
>
>
> import sqlalchemy
> from sqlalchemy.ext.declarative import declarative_base
> from sqlalchemy import Integer
> from sqlalchemy import Column
> from sqlalchemy import ForeignKey
> from sqlalchemy.orm import relationship
> from sqlalchemy import create_engine
> from sqlalchemy.orm import sessionmaker
>
>
> # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> - - -
> Base = declarative_base()
> # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> - - -
>
> class A(Base):
> __tablename__ = 'a'
> id = Column(Integer, primary_key=True)
>
>
> cs = relationship(
> "C",
> primaryjoin="A.id == B.a_id",
> secondary="join(B, B2C, B.id == B2C.b_id)."
>   "join(B2C, C, B2C.c_id == C.id)",
> # secondaryjoin="and_(C.id == B2C.c_id)",
> viewonly=True
> )
>
>
> class B(Base):
> __tablename__ = 'b'
> id = Column(Integer, primary_key=True)
> a_id = Column(ForeignKey('a.id'))
>
>
> class B2C(Base):
> __tablename__ = 'b2c'
>
> id = Column(Integer, primary_key=True)
> b_id = Column(ForeignKey('b.id'))
> c_id = Column(ForeignKey('c.id'))
>
>
> class C(Base):
> __tablename__ = 'c'
> id = Column(Integer, primary_key=True)
>
>
> engine = create_engine("sqlite://", echo=True)
> Base.metadata.create_all(engine)
> sessionFactory = sessionmaker(bind=engine)
> sess = sessionFactory()
>
> sess.query(A).join(a.cs).all()
>
>
> --
> 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 view this discussion on the web visit
> https://groups.google.com/d/msgid/sqlalchemy/474ccb9b-6839-47b7-9d38-fd1a7065f7a4%40googlegroups.com
> 
> .
>


-- 

Elmer

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/CAA7D1eGmrtoGYwXO18z%2BbEfc%3DR2-_F30uXq%2BKu6ykLjAyDGviA%40mail.gmail.com.


[sqlalchemy] composite secondary join variant

2020-04-23 Thread Jonathan Vanasco
i'm stuck on a variant of the Composite Secondary Join (
https://docs.sqlalchemy.org/en/13/orm/join_conditions.html#composite-secondary-joins
)

I hope someone can see what my tired eyes are missing. I'm fairly certain 
the issue is in `secondary' and 'secondaryjoin'.  I've tried a handful of 
variants I thought would work, but trigger this error:

sqlalchemy.exc.InvalidRequestError: Class  does not 
have a mapped column named 'get_children'

I've used this pattern with much luck in the past: `primaryjoin` goes from 
the base class to whatever I build the secondary/secondary join from. 

I've distilled the relationship pattern as below:

* `A` does not fkey onto anything.
* `B` fkeys onto `A`
* The intended relationship on `A` is a list of `C` items, which are 
related to `B` through an association table

I can build out a the relationship from B, and I could probably mount it 
onto A with an association_proxy, but I'd like to understand what I'm doing 
wrong with the `relationship` off A.  This is puzzling me.

Thanks in advace.


import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Integer
from sqlalchemy import Column
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
- -
Base = declarative_base()
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
- -

class A(Base):
__tablename__ = 'a'
id = Column(Integer, primary_key=True)


cs = relationship(
"C",
primaryjoin="A.id == B.a_id",
secondary="join(B, B2C, B.id == B2C.b_id)."
  "join(B2C, C, B2C.c_id == C.id)",
# secondaryjoin="and_(C.id == B2C.c_id)",
viewonly=True
)


class B(Base):
__tablename__ = 'b'
id = Column(Integer, primary_key=True)
a_id = Column(ForeignKey('a.id'))


class B2C(Base):
__tablename__ = 'b2c'

id = Column(Integer, primary_key=True)
b_id = Column(ForeignKey('b.id'))
c_id = Column(ForeignKey('c.id'))


class C(Base):
__tablename__ = 'c'
id = Column(Integer, primary_key=True)


engine = create_engine("sqlite://", echo=True)
Base.metadata.create_all(engine)
sessionFactory = sessionmaker(bind=engine)
sess = sessionFactory()

sess.query(A).join(a.cs).all()


-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/474ccb9b-6839-47b7-9d38-fd1a7065f7a4%40googlegroups.com.