Once you load the object with get(), it's done, it's in the session's
identity map and doing another get() on it will just give you back that
same object from memory without emitting SQL.   If you want to reload it
you either have to session.expire() it, session.close() /
session.expire_all() to get rid of it first, or use
query.populate_existing().

Also, the "noload" option is very seldom used as it does not prevent you
from accessing the relationship, you just get a blank result which is
wrong.  If you want these to raise an error on access I'd recommend using
raiseload instead.

On Tue, Jul 3, 2018, 6:46 PM <phil.nac...@freenome.com> wrote:

> Hello,
>
> I'm encountering an issue where loading a model in a text fixture causes
> the load options to be ignored when I load the same model in a Flask
> endpoint
>
> I have these two models
>
> class Plate(db.Model):
>     container_id = db.Column(db.Integer, db.ForeignKey('containers.id'))
>     container = db.relationship('Container', uselist=False, lazy='noload')
>
> class Container(db.Model):
>     ...
>
>
> The relationship is 'noload', since that's the default relationship
> loading technique I want.
>
> However, I have an endpoint to update a plate in which I want the plate's
> container to be loaded. In the endpoint, I do this:
>
> plate = Plate.query.options(noload('*'), joinedload(Plate.container).
> noload('*')).get(args['plate_id']
>
> Here, plate.container is correctly loaded.
>
> However, in a test, I have a fixture which POSTs a plate, and then loads
> the plate.
>
> @pytest.fixture(scope='function')
> def plate_fixture(client, session):
>     response = client.post('/plate-endpoint', content_type=... data=...)
>     plate = Plate.query.get(response.json['plate_id']
>     return plate
>
>
> In the same endpoint code above, plate.container is not loaded for tests,
> but is loaded correctly in the app. Removing Plate.query.get from the
> fixture fixes the issue.
>
>
> I'm using SQLAlchemy 1.1.5 (although I've tried 1.2.9 and it hasn't fixed
> the issue), Postgres 9.6, and pytest-postgresql 1.3.4
>
> Thanks for the help
>
>
>
> This e-mail is private and confidential and is for the addressee only. If
> misdirected, please notify us by telephone, confirming that it has been
> deleted from your system and any hard copies destroyed. You are strictly
> prohibited from using, printing, distributing or disseminating it or any
> information contained in it save to the intended recipient.
>
> --
> 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 sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> 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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to