Relationship always returns None when accessed

35 views
Skip to first unread message

Tucker Buchy

unread,
Aug 13, 2014, 1:22:37 PM8/13/14
to sqlal...@googlegroups.com
Hi all, 

So I'm trying to do something fairly standard, I'm creating a db mapped object which has a many-to-one relationship to another db mapped class. I'm basically "inheriting" and bunch of attribute values from that relationship and so I want to populate those fields by reading from the relationship and populate them with the appropriate values found in that relationship. The issue is is that the relationship always seems to be returning None, which is strange seeing as when I do a straight session.query for that object it is definitely able to get it. 

Here is the code I'm talking about:

def create(record):
        parent = record.acquisition_request # <----- Parent is always None here!
        record.product_type = parent.product_type
        record.line_item = parent.line_item

        session = get_scoped_session()

        session.add(record)
        try:
            session.commit()
        except:
            session.rollback()
            raise

And here is the relationship constructor that I am trying to access:

acquisition_request = relationship("AcquisitionActive",
                                   primaryjoin='AcquisitionActive.acquisition_request_id == AcquisitionOpportunityActive.acquisition_request_id',
                                   backref="acquisition_opportunities",
                                   foreign_keys=acquisition_request_id, uselist=False)

Wondering if it's something that can't be resolved before a db flush or something? Doesn't seem right as I am not querying on any surrogate key or anything, just some a foreign key (which is present in the object).

Hope this is enough information, thanks for any help you guys have.

Michael Bayer

unread,
Aug 13, 2014, 2:06:52 PM8/13/14
to sqlal...@googlegroups.com
what SQL and result set do you see being emitted if you use echo=‘debug’ with your engine (or DEBUG logging with “sqlalchemy.engine” ) ?   that will show just what queries are emitted and what data is being returned.   I suggest combining this with pdb.set_trace() in the appropriate section so that you can zoom in on where it should be loading.


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

Reply all
Reply to author
Forward
0 new messages