[Nitro] og: automatic deletion of records in M:N relations?

Stephan Mueller d454d at web.de
Sun Aug 27 03:57:42 EDT 2006


Hello again,

* Jonathan Buch <john at oxyliquit.de> [23.08.2006]:
> > Try to set the relation only to other of the classes (it's two-way
> > automatically).
> 
> > so Tags get deleted if the Item is deleted, I don't know why you would
> > want to remove the Items if a Tag is removed...
> 
> Well, with Tags you're quite right, but you may think of a online shop,  
> where you have a `ShoppingCart` and `ShoppingCartItem`'s.  When the User  
> shops and suddenly logs out, the easiest thing to do is just to delete the  
> shopping cart, which will then 'cascade' and delete the other items as  
> well.

I think I did not make my point clear: My problem ist not wether tags or
items get removed according to their relation. The problem ist the
mapping table og creates to maintain many to X relations. Entries in
this table do not get removed, even if neccesary. 

I added some code to my previous example, to show the real problem:

>>>>
#!/usr/bin/ruby
#

require 'rubygems'
require 'og'

class Item
  property :name, String

  joins_many Tag

  def to_s
    @name
  end
end

class Tag
  property :name, String

  def to_s
    @name
  end
end

Og.setup(:store => :sqlite, :name => 'm_n')

(1..3).each { | n |
  t = Tag.new
  t.name = "Tag_" + n.to_s
  t.save
  puts "created tag with oid " + t.oid.to_s
}

# see if i can get a tag back from th db
t = Tag.find_by_name("Tag_1")
t.nil? ? puts("no tag found") : puts("found tag: " + t.name)

i1 = Item.new
i1.name = "Item_1"
i1.save
i1.add_tag(Tag.find_by_name("Tag_1"))
i1.add_tag(Tag.find_by_name("Tag_2"))
i1.save

i2 = Item.new
i2.name = "Item_2"
i2.save
i2.add_tag(Tag.find_by_name("Tag_2"))
i2.add_tag(Tag.find_by_name("Tag_3"))
i2.save

puts "please inspect db now :)\n"
gets

i1.delete
i2.delete

puts "and again...\n"

# the following code is new an triggers an exception due to existing
# items in the mapping table

i3 = Item.new
i3.name = "Item_3"
i3.save
i3.add_tag(Tag.find_by_name("Tag_1"))
i3.add_tag(Tag.find_by_name("Tag_2"))
i3.save
>>>>

This results in the folowing exception:

% ruby m_n_with_og.rb
 INFO: Og uses the Sqlite store.
 INFO: Created table 'ogtag'.
 INFO: Created table 'ogitem'.
created tag with oid 1
created tag with oid 2
created tag with oid 3
found tag: Tag_1
please inspect db now :)

and again...
/usr/lib/ruby/1.8/sqlite3/errors.rb:94:in `check': SQL logic error or missing database (SQLite3::SQLException)
        from /usr/lib/ruby/1.8/sqlite3/resultset.rb:76:in `check'
        from /usr/lib/ruby/1.8/sqlite3/resultset.rb:68:in `commence'
        from /usr/lib/ruby/1.8/sqlite3/resultset.rb:61:in `initialize'
        from /usr/lib/ruby/1.8/sqlite3/statement.rb:158:in `execute'
        from /usr/lib/ruby/1.8/sqlite3/database.rb:274:in `query'
        from /usr/lib/ruby/gems/1.8/gems/og-0.31.0/lib/og/store/sqlite.rb:92:in `exec'
        from /usr/lib/ruby/gems/1.8/gems/og-0.31.0/lib/og/store/sql.rb:582:in `join'
        from (eval):26:in `add_tag'
        from m_n_with_og.rb:63


> Right now I have this idea in my head that Og could utilize information  
>  from the DB or simulate when the DB doesn't support constraints.

I think this does not apply to my problem but in general i like your
idea. On the other hand we already have information about these
constraints due to the knowledge about their relation. 

> Feel the love
> http://pinkjuice.com/pics/ruby.png

nice picture! :)


Cheers,

Steph.


More information about the Nitro-general mailing list