On Sep 28, 2010, at 4:01 PM, Michael Bayer wrote:

> 
> On Sep 28, 2010, at 3:28 PM, Mark Erbaugh wrote:
> 
>> I have a self-referential table:
>> 
>> class L3Acct(BASE):
>> 
>>   __tablename__ = 'l3_acct'
>> 
>>   id = Column(Integer, primary_key=True)
>>   parent = Column(ForeignKey('l3_acct.id'))
>> 
>>   [....]
>> 
>> 
>> When adding new rows to the table, the id field is not assigned a value 
>> until the data is actually written to the database.  When adding several 
>> rows to a session object is there a way for a new row to reference a row 
>> that has previously been added in the same batch, but hasn't been assigned 
>> an id yet?
> 
> sure, you use the relation() function at the class level to associate L3Acct 
> instances with their parent instance, and establish the linkage at the object 
> level, rather than the pk/fk level.
> 
> This would be many-to-one, make sure you follow whats at 
> http://www.sqlalchemy.org/docs/orm/relationships.html#adjacency-list-relationships
>  carefully (including the remote_side flag).   Those are the 0.6 docs but 
> configuration for 0.5.8 is the same (except relationship() is relation()).
> 

Michael,

Thanks.  I've already used a relation to add child records to a separate table, 
I hadn't thought of doing that with the same table.  I'm still trying to get my 
head out of directly working with SQL and working with the ORM.

Mark

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

Reply via email to