[Dbix-class] table referencing itself
Oliver Gorwits
oliver.gorwits at oucs.ox.ac.uk
Wed Dec 20 21:27:29 GMT 2006
Daniel McBrearty wrote:
> CREATE TABLE whatever (
> id SERIAL PRIMARY KEY,
> whatever_id INTEGER NOT NULL REFERENCES whatever(id),
>
> Is there a way to get the id of the object being created in the
> statement doing the creation? I don't even know if the db (postgresql)
> can do that ... ?
id SERIAL makes the col base itself on a (probably auto-generated)
sequence. you can just ask the sequence for the next integer, and use
that in the create.
sequences keep issuing integers, never repeat themselves, and are
independent of the cols based upon them (so you can do things like get
the next seq id, then abort a transaction and rollback, but the next
issued id will still be after that last issued, but aborted, one).
to find the sequence name go to the psql shell and type \d <return> and
\d whatever <return> (for table "whatever").
not sure what magic DBIC has to aid the above, but I wouldn't be
surprised if something was in there somewhere to help you :)
regards,
oliver.
--
Oliver Gorwits, Network Infrastructure Group,
Oxford University Computing Services
More information about the Dbix-class
mailing list