On Feb 26, 2011, at 4:17 AM, Chris Withers wrote:

> On 25/02/2011 17:13, Michael Bayer wrote:
>> can't reproduce.  See attached.    This test includes randomization of all 
>> key data structures in the UOW which smokes out any issues in dependency 
>> sorting.
> 
> Okay, so after about an hour of stripping down code, please find attached a 
> minimal reproducible test case.

this is a simple mistake.  This:

session.add(Grant(username=user.username))

is this:

x = Parent(id=1)
y = Child(parent_id=1)

--> commit

How is x.children to know that "y" is in any way related to x ?   "parent_id" 
is a database column, SQLA populates these but never interprets their 
relationship to other columns in Python.   The database is used to reconcile 
individual foreign key values with their parent rows based on generated SQL, 
the rows then come back.    

A very old FAQ entry which is related to this is at:

http://www.sqlalchemy.org/trac/wiki/FAQ#Isetthefoo_idattributeonmyinstanceto7butthefooattributeisstillNone-shouldntithaveloadedFoowithid7

this entry could perhaps be expanded upon someday to explain the basic nature 
of how the ORM resolves relationships.






> 
> As noted in the comments, if sessionmaker isn't used, all is well.
> 
> Even if sessionmaker is used, if the user is passed to Grant instead of the 
> username, all is well.
> 
> However, if you pass the username *and* use sessionmaker, you'll 
> intermittently get:
> 
> IntegrityError: (IntegrityError) (1452, 'Cannot add or update a child row: a 
> foreign key constraint fails (`acl_tests`.`grant`, CONSTRAINT `grant_ibfk_1` 
> FOREIGN KEY (`username`) REFERENCES `user` (`username`) ON DELETE CASCADE)') 
> 'INSERT INTO `grant` (username) VALUES (%s)' ('testname',)
> 
> ...as the insert of the grant is tried before the insert of the user.
> 
> I'm using:
> 
> Python 2.6.6
> SQLAlchemy 0.6.6
> MySQL_python 1.2.3
> 
> The MySQL server is running version 5.1.49-3 of debian's mysql-server package.
> 
> Good luck!
> 
> Chris
> 
> -- 
> Simplistix - Content Management, Batch Processing & Python Consulting
>           - http://www.simplistix.co.uk
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 
> <test_bug.py>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to