On Wed, Nov 6, 2013 at 11:48 AM, Victor Varvariuc
<victor.varvar...@gmail.com> wrote:
> How can this be? Is this the intended behavior?
>
> ipdb> customer = factory.new_customer(_with_id=True)
> ipdb> with Session() as session: customer = session.merge(customer)
> ipdb> customer
> Customer(id=100000002, first_name=u'Testfirstname100000002',
> last_name=u'Testlastname100000002', middle_name=u'Testmiddlename100000002',
> email='test100000...@moda.ru', password='qDV6Za', gender='M',
> phone='00100000002', is_active=True, is_deleted=False)
> ipdb> subscription = factory.new_subscription(_with_id=True)
> ipdb> subscription
> Subscription(id=100000001, email='test100000...@lamoda.ru',
> status='UNSUBSCRIBED', sms_status='UNSUBSCRIBED',
> confirm_code='jFw3blG7UD3BdCIPqZ9VacCBEno8NtOOYhfLXef3QvmIQyfvC3',
> gender=None)
> ipdb> subscription.customer = customer
> ipdb> subscription.customer
> Customer(id=100000002, first_name=u'Testfirstname100000002',
> last_name=u'Testlastname100000002', middle_name=u'Testmiddlename100000002',
> email='test100000...@moda.ru', password='qDV6Za', gender='M',
> phone='00100000002', is_active=True, is_deleted=False)
> ipdb> subscription.customer_id
>
> `customer` object was committed to the database, it has an id. Why
> `subscription.customer` is set, but `subscription.customer_id` is not?
>
> Using SQLAlchemy 0.8.3
>
> class Customer(Base):
>
>     id = Column(BigInteger, primary_key=True, autoincrement=False)
>
> ...
>
> class Subscription(Base):
>
>     id = Column(BigInteger, primary_key=True, autoincrement=False)
>     customer_id = Column(Integer, ForeignKey(Customer.id,
> ondelete='CASCADE'))
>     customer = relationship(Customer, lazy='joined')
>

I think the answer is at this FAQ:

  
<http://docs.sqlalchemy.org/en/latest/faq.html#i-set-the-foo-id-attribute-on-my-instance-to-7-but-the-foo-attribute-is-still-none-shouldn-t-it-have-loaded-foo-with-id-7>

Your situation is the opposite way round (you are setting the
relationship and then inspecting the foreign key, rather than setting
the foreign key and inspecting the relationship), but I suspect the
answer is the same: these attributes aren't changed until you call
session.flush()

Simon

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to