[Nitro] Foreign key constraints

Brian Candler B.Candler at pobox.com
Tue Aug 23 16:44:55 EDT 2005

Question 1: would it be possible for Og to insert foreign key constraints
automatically? I'm thinking particularly of the 'belongs_to' and
'many_to_many' relationships.

For example:

    class Tree; end

    class Tree
        property :name, String
        belongs_to :parent, Tree

IMO this ought to do an:

    ALTER TABLE ogtree ADD FOREIGN KEY oid_parent_oid
                       (oid) REFERENCES tree (parent_oid);

If this sort of stuff has to be done by hand on the SQL databases after the
fact, then it doesn't really seem worth having Og create the tables
automatically from the metadata; it might make more sense to create the SQL
tables manually, and generate the class definitions from that. (Although
some people might prefer to ignore database integrity checks for convenience
of programming, I don't think that's a good way to write a robust

Aside: there's a slight bootstrapping problem with this particular example,

    root = Tree.new 
    root.parent = root 

inserts an entry with oid=1 but parent=NULL. I would like to have a not-null
constraint on parent too; "belongs_to" becomes "must_belong_to" :-) But in
the case of a tree, I don't mind having to manually send some SQL to insert
the root node:

    insert into ogtree (oid,parent) VALUES (1,1);

Question 2: Are transactions implemented at all? Can someone give me some
example code showing how they would be used?

I note they are not implemented for mysql:

  def commit
    # nop, not supported?
    # FIXME: InnoDB supports transactions.

However it *is* possible to create the correct table types:

  db = Og.setup(
      :store    => 'mysql',
      :name     => 'test',
      :user     => 'root',
      :password => '',
      :table_type => 'innoDB'

Having both referential integrity and transactions is I think important for
more complex applications, when inserting multiple related objects.



More information about the Nitro-general mailing list