On Thu, Aug 17, 2017 at 3:57 PM, cecemel <[email protected]> wrote:
> Is the backref relationship supposed to work with objects loaded from the
> DB?
>
>
> Here is the case:
>
> Similar to the (doc/tutorial) I have the following classes:
>
> from sqlalchemy import Integer, ForeignKey, String, Column
> from sqlalchemy.ext.declarative import declarative_base
> from 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!!
> []
that is supposed to work, yes, what you illustrate above is not the
expected behavior.
>
> >>> a1.user
this would be "u1".
>
> None
>
> So my questions:
>
> - is this supposed to work?
yes.
> - if so, how can I make it work?
I don't know because you are likely doing something unexpected in your
actual example. It seems likely that your get_user_by_id_from_id()
function does not accept a "session" as an argument makes it suspect
where it's getting the Session from.
Here's the MCVE:
from sqlalchemy import Integer, ForeignKey, String, Column, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, Session
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")
e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)
s = Session(e)
s.add(User(id=1))
s.commit()
def get_user_by_id_from_db(id):
return s.query(User).get(id)
u1 = get_user_by_id_from_db(1)
a1 = Address()
u1.addresses # fair, the user didn't have any address.
u1.addresses.append(a1)
assert u1.addresses == [a1]
assert a1.user is u1
> - 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.
--
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.