Is the backref relationship supposed to work with objects loaded from the 
DB?


Here is the case: 

Similar to the (doc/tutorial 
<http://docs.sqlalchemy.org/en/rel_1_1/orm/backref.html?highlight=backref>) 
I have the following classes:

from sqlalchemy import Integer, ForeignKey, String, Columnfrom 
sqlalchemy.ext.declarative import declarative_basefrom sqlalchemy.orm import 
relationship
Base = declarative_base()
class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)
    name = Column(String)

    addresses = relationship("Address", back_populates="user")
class Address(Base):
    __tablename__ = 'address'
    id = Column(Integer, primary_key=True)
    email = Column(String)
    user_id = Column(Integer, ForeignKey('user.id'))

    user = relationship("User", back_populates="addresses")

When testing the snippet it works like expected:

>>> u1 = User()>>> a1 = Address()>>> u1.addresses[]>>> print(a1.user)None
>>> u1.addresses.append(a1)>>> u1.addresses[<__main__.Address object at 
>>> 0x12a6ed0>]>>> a1.user<__main__.User object at 0x12a6590>

But when doing the same with a user loaded to the db:

    >>> u1 = get_user_by_id_from_db(1)
    >>> a1 = Address()
    >>> u1.addresses # fair, the user didn't have any address.
    []
    >>> u1.addresses.append(a1)
    >>> u1.addresses # I didn't expect empty list!!
    []

    >>> a1.user

    None

So my questions:

   - - is this supposed to work?
   - - if so, how can I make it work?
   - - if not, any workarounds proposed (and why not)?

More information about the system:

SQLAlchemy 1.1.13

Python 3.5

Postgres 9.6



thanks again!


Please let me know if any details miss.


-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to