On 09/28/2010 02: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?
>
> I'm using SQLAlchemy 0.5.8.
>
> Thanks,
> Mark
>
You can do this if there is a relationship between the parent/child
L3Acct objects, e.g.:
class L3Acct(BASE):
[..existing declarations..]
parent_obj = relation("L3Acct", backref="children", remote_side=[id])
Then you can add the object to the session without using ids:
parent = L3Acct()
child = L3Acct(parent_obj=parent)
session.add(parent) # child is added implicitly
session.flush()
The ORM will take care of inserting parents before children.
-Conor
--
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.