The mechanism of backrefs is such that when you assign to the many-to-one side of the relationship, if the one-to-many is not loaded from the database, the "append" that you want to do does not occur at that time. When the list is not loaded, the object is placed in a queue where it will be appended to the list, if and when that list is actually loaded from the database, otherwise it is just cleared when the session is next flushed (because the SQL load of that list will now return the newly appended value as well).

This behavior is because "append to a list" requires that the list be loaded from the database and long ago this behavior was established to eliminate the problem of many/huge list loads just by setting attributes.

So if your list is doing something critical, like setting other database attributes when something happens to the list in memory, you would need to ensure the list is appended towards explicitly. Otherwise, setting up attributes like this is better addressed at flush time in a handler such as before_flush().



On 09/23/2016 01:03 PM, HP3 wrote:
There seems to be something else ...

The following test passes which indicates that position is set as 0
right after the bullet and the slide are related:

 93     deftest_backref_set(self):

 94         self._setup(ordering_list('position'))

 95

 96         #session = create_session()

 97         s1 = Slide('Slide #1')

 98         #session.add(s1)

 99         #session.flush()

100

101         self.assert_(nots1.bullets)

102         self.assert_(len(s1.bullets) == 0)

103

104         b0 = Bullet('s1/b0')

105         assertb0.position == None

106         #session.add(b0)

107         b0.slide = s1

108         assertb0.position == 0

109

110         #session.flush()

111

112         self.assert_(s1.bullets)

113         self.assert_(len(s1.bullets) == 1)

114         self.assert_(s1.bullets[0] == b0)

115         self.assert_(s1.bullets[0].position == 0)



--
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
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto: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.

Reply via email to