On Tue, Sep 4, 2018 at 11:00 PM, Alex Rothberg <agrothb...@gmail.com> wrote:
> I tracked down the error on my side. Looks like I have to use the table name
> rather than the model name (doh) in the string. That being said, there may
> still be a bug in sqla where it tries to read the name off a join (rather
> than a table).
>
> That being said, any reason not to support the lambda syntax for
> ForeignKeyConstraint rather than just the string syntax?

the lambda thing currently is specific to Declarative and the ORM.   I
guess FKC could do sometihng similar as well but i would need a
contributor to work on writing some tests and the implementation for
this.    lambdas for declarative are useful because we sometimes
prefer that when writing out a big primaryjoin for relationship() or
something like that.   the remote columns for an FKC OTOH are just
those names.   Although, it does make it possible to have FKCs across
two MetaData objects that were declared at different times...that
actually is useful.    I'm interested in alternatives to the whole
MetaData thing someday and perhaps using lambdas instead of strings
could be part of it.


>
>
> On Tuesday, September 4, 2018 at 10:43:13 PM UTC-4, Alex Rothberg wrote:
>>
>> You're right the error I posted is coming from somewhere else. I am trying
>> to get a stripped down example. In the meantime, it looks like when I add
>> the additional fk constraint, model.__mapper__.get_property(property_name)
>> on a different model starts failing.
>>
>>   File
>> "/Users/alex/.pyenv/versions/api2/lib/python3.7/site-packages/marshmallow_sqlalchemy/convert.py",
>> line 151, in field_for
>>     prop = model.__mapper__.get_property(property_name)
>>   File
>> "/Users/alex/.pyenv/versions/api2/lib/python3.7/site-packages/sqlalchemy/orm/mapper.py",
>> line 1923, in get_property
>>     configure_mappers()
>>   File
>> "/Users/alex/.pyenv/versions/api2/lib/python3.7/site-packages/sqlalchemy/orm/mapper.py",
>> line 3033, in configure_mappers
>>     mapper._post_configure_properties()
>>   File
>> "/Users/alex/.pyenv/versions/api2/lib/python3.7/site-packages/sqlalchemy/orm/mapper.py",
>> line 1832, in _post_configure_properties
>>     prop.init()
>>   File
>> "/Users/alex/.pyenv/versions/api2/lib/python3.7/site-packages/sqlalchemy/orm/interfaces.py",
>> line 183, in init
>>     self.do_init()
>>   File
>> "/Users/alex/.pyenv/versions/api2/lib/python3.7/site-packages/sqlalchemy/orm/relationships.py",
>> line 1656, in do_init
>>     self._setup_join_conditions()
>>   File
>> "/Users/alex/.pyenv/versions/api2/lib/python3.7/site-packages/sqlalchemy/orm/relationships.py",
>> line 1731, in _setup_join_conditions
>>     can_be_synced_fn=self._columns_are_mapped
>>   File
>> "/Users/alex/.pyenv/versions/api2/lib/python3.7/site-packages/sqlalchemy/orm/relationships.py",
>> line 1998, in __init__
>>     self._determine_joins()
>>   File
>> "/Users/alex/.pyenv/versions/api2/lib/python3.7/site-packages/sqlalchemy/orm/relationships.py",
>> line 2082, in _determine_joins
>>     consider_as_foreign_keys=consider_as_foreign_keys
>>   File "<string>", line 2, in join_condition
>>   File
>> "/Users/alex/.pyenv/versions/api2/lib/python3.7/site-packages/sqlalchemy/sql/selectable.py",
>> line 964, in _join_condition
>>     a, a_subset, b, consider_as_foreign_keys)
>>   File
>> "/Users/alex/.pyenv/versions/api2/lib/python3.7/site-packages/sqlalchemy/sql/selectable.py",
>> line 1021, in _joincond_scan_left_right
>>     if nrte.table_name == b.name:
>> AttributeError: 'Join' object has no attribute 'name'
>>
>>
>> On Tuesday, September 4, 2018 at 9:40:11 PM UTC-4, Mike Bayer wrote:
>>>
>>> On Tue, Sep 4, 2018 at 7:54 PM, Alex Rothberg <agrot...@gmail.com> wrote:
>>> > Is it possible to set up a `ForeignKeyConstraint` that uses a class not
>>> > yet
>>> > declared? ie is there a way to use either the lambda or string syntax
>>> > to
>>> > forward declare the fk constrains? Neither works for me. Using strings
>>> > yields:
>>> >
>>> >   File "<string>", line 2, in join_condition
>>> >   File
>>> >
>>> > "/Users/alex/.pyenv/versions/api2/lib/python3.7/site-packages/sqlalchemy/sql/selectable.py",
>>> > line 964, in _join_condition
>>> >     a, a_subset, b, consider_as_foreign_keys)
>>> >   File
>>> >
>>> > "/Users/alex/.pyenv/versions/api2/lib/python3.7/site-packages/sqlalchemy/sql/selectable.py",
>>> > line 1021, in _joincond_scan_left_right
>>> >     if nrte.table_name == b.name:
>>> > AttributeError: 'Join' object has no attribute 'name'
>>> >
>>> > and I can't get the lambda form to work.
>>> > I tried:
>>> > db.ForeignKeyConstraint((employee_id, year, home_fund_id),
>>> > ('FundEmployee.employee_id', 'FundEmployee.year',
>>> > 'FundEmployee.fund_id'))
>>>
>>> ForeignKeyConstraint can be fully declared with just strings and the
>>> referenced table and/or declarative class doesn't need to exist yet,
>>> see
>>> http://docs.sqlalchemy.org/en/latest/core/constraints.html#metadata-foreignkeys.
>>>    That AttributeError doesn't seem to be raised by a
>>> ForeignKeyConstraint, looks like it's coming from orm.relatiionship or
>>> something.   Feel free to provide a more complete example of what
>>> you're trying to do.
>>>
>>>
>>> >
>>> >
>>> > --
>>> > 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.
>>> > To post to this group, send email to sqlal...@googlegroups.com.
>>> > 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.

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

Reply via email to