On Mon, Feb 5, 2018 at 8:40 AM, Edu Ferreira <edugn...@gmail.com> wrote: > Hello, i implemented a generic foreign key/relationship, using this example: > http://docs.sqlalchemy.org/en/latest/_modules/examples/generic_associations/discriminator_on_association.html > > In the example addresses is passing in constructor: > > session.add_all([ > Customer( > name='customer 1', > addresses=[ > Address( > street='123 anywhere street', > city="New York", > zip="10110"), > Address( > street='40 main street', > city="San Francisco", > zip="95732") > ] > ), > Supplier( > company_name="Ace Hammers", > addresses=[ > Address( > street='2569 west elm', > city="Detroit", > zip="56785") > ] > ), > ]) > > > > But if i try to do something like this: > > _cust = Customer('name') > session.add(_cust) > session.commit() > > #And After try to use "addresses" > > _cust.addresses.append(Address(street='2569 west elm',city="Detroit", > zip="56785")) > #It doens't work, "addresses" is None. > > > Is there any way to use this without pass "addresses" in the constructor? > I'm using mysql and sqlachemy version is '1.1.15'.
the proxy is being used slightly oddly here, in that Customer.address_association is a scalar but then Customer.address_association.addresses is a list. The proxy isn't smart enough to figure out that it needs to initialize a new CustomerAddressAssociation when you say something like customer.addresses.append. So you need to set one up by assigning first: customer.addresses = [], now there is a customer.address_association set up and append() will work . You can do this assignment in the constructor of Customer if you want. I never use this style, I always use table_per_related or table_per_association. > > -- > 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.