I'm writing some tests for my system, and I have all the objects that I 
*would* be getting from the database, as flat json files, which I then load 
up from, to SQLAlchemy files for my tests.

I've all that working fine, but the one place I'd like to improve is 
"gluing" them back together / creating the mapping associations.

Consider the following,

class Sport(Base):
    id = Column(Integer, primary_key=True)
    name = Column(String(50), unique=True)

class Competition(Base):
    id = Column(Integer, primary_key=True)
    name = Column(String(50), unique=True)

    sport_id = Column(Integer, ForeignKey(Sport.id))
    sport = relationship(Sport)


If I run 

sport = Sport.query.filter(Sport.id == 1).first()
comp = Competition.query.filter(Competition.sport == sport)
assert sport == comp.sport


Everything's good.

However, if I create a Competition as a "flat" object,

comp2 = Competition(id=2345, name='My Comp', sport_id=1)
session.add(comp2)
assert sport == comp2.sport


This one fails, because *sport* on *comp2* is null.

Poking through the code, I see it's mainly / probably to do with the logic 
for _load_for_state in strategies,

def _load_for_state(self, state, passive):

    if not state.key and (
            (
                not self.parent_property.load_on_pending
                and not state._load_pending
            )
            or not state.session_id
    ):
        return attributes.ATTR_EMPTY


comp has a *state.key* for it's sport column, whereas comp2 doesn't (and 
both self.parent_property.load_on_pending and state._load_pending are false.

So yeah, that's my overall question.  With an object that's *not* gone 
into, nor come back from the database, is there some global option, or 
something along those lines so that when I check properties of those 
objects, the mapper does its thing, and maps to its objects, and not 
*ATTR_EMPTY 
/ None*?


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