Re: [sqlalchemy] Weird SELECT when assigning to one-to-one relationship

2016-02-19 Thread Adrian
Thought something like that.. i did actually find that it's the backref 
causing it by stepping through tons of SA code ;)
So I guess setting it to None explicitly on creation is the correct way to 
avoid the SELECT?

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


Re: [sqlalchemy] Weird SELECT when assigning to one-to-one relationship

2016-02-19 Thread Mike Bayer
so this is uselist=False e.g. one-to-one, you make a Contribution with a 
non-present timetable entry, when you get to the flush(), it knows 
nothing about what timetable_entry objects might exist for this row.


Unsurprisingly then, if we do this:

contrib = Contribution()
s.add(contrib)
s.flush()
contrib.timetable_entry


that last line emits a SELECT.  Not surprising, right?   The assignment 
of "tte.contribution" similarly sets up the backref which necessarily 
needs to replace any previous timetable entry object, so it has to lazy 
load to see if there is one.


Stick a pdb into where it lazyloads and the stack trace should give 
clues about these (when you see "fire_replace_event", you see, ah, it's 
replacing an old value and it needs to find it):


> 
/home/classic/dev/sqlalchemy/lib/sqlalchemy/orm/strategies.py(559)_emit_lazyload()

-> q = session.query(self.mapper)._adapt_all_clauses()
(Pdb) where
  /home/classic/dev/sqlalchemy/test.py(47)()
-> tte.contribution = contrib

/home/classic/dev/sqlalchemy/lib/sqlalchemy/orm/attributes.py(224)__set__()
-> instance_dict(instance), value, None)
  /home/classic/dev/sqlalchemy/lib/sqlalchemy/orm/attributes.py(807)set()
-> value = self.fire_replace_event(state, dict_, value, old, initiator)

/home/classic/dev/sqlalchemy/lib/sqlalchemy/orm/attributes.py(829)fire_replace_event()
-> self._replace_token or self._init_append_or_replace_token())

/home/classic/dev/sqlalchemy/lib/sqlalchemy/orm/attributes.py(1183)emit_backref_from_scalar_set_event()
-> passive=PASSIVE_NO_FETCH)

/home/classic/dev/sqlalchemy/lib/sqlalchemy/orm/attributes.py(608)append()
-> self.set(state, dict_, value, initiator, passive=passive)
  /home/classic/dev/sqlalchemy/lib/sqlalchemy/orm/attributes.py(790)set()
-> state, dict_, passive=PASSIVE_ONLY_PERSISTENT | NO_AUTOFLUSH)
  /home/classic/dev/sqlalchemy/lib/sqlalchemy/orm/attributes.py(583)get()
-> value = self.callable_(state, passive)

/home/classic/dev/sqlalchemy/lib/sqlalchemy/orm/strategies.py(532)_load_for_state()
-> return self._emit_lazyload(session, state, ident_key, passive)
  (1)()
> 
/home/classic/dev/sqlalchemy/lib/sqlalchemy/orm/strategies.py(559)_emit_lazyload()

-> q = session.query(self.mapper)._adapt_all_clauses()
(Pdb)






On 02/19/2016 04:34 AM, Adrian wrote:

Check this testcase:
https://gist.github.com/ThiefMaster/913446490d0e4c31776d

When assigning an object to the relationship attribute a SELECT is sent, but
this does not happen when explicitly setting the attribute to None
before assigning
the object to it.

If the SELECT being issued is not a bug, is initializing the attribute
with an explicit
`None` the proper way to avoid it?

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


--
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] Weird SELECT when assigning to one-to-one relationship

2016-02-19 Thread Adrian
Check this testcase: 
https://gist.github.com/ThiefMaster/913446490d0e4c31776d

When assigning an object to the relationship attribute a SELECT is sent, but
this does not happen when explicitly setting the attribute to None before 
assigning
the object to it.

If the SELECT being issued is not a bug, is initializing the attribute with 
an explicit
`None` the proper way to avoid it?

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