#625 ✓resolved
Lawrence Pit

[PATCH] many-to-many association replace method bug

Reported by Lawrence Pit | October 30th, 2008 @ 06:22 PM

class Article
  include DataMapper::Resource
  property :id, Serial
  property :title, String, :nullable => false
  has n, :categories, :through => Resource
end

class Category
  include DataMapper::Resource
  property :id, Serial
  property :name, String, :nullable => false
  has n, :articles, :through => Resource
end
 
DataMapper.auto_migrate!
 
a1 = Article.create(:title => "foo1")
a2 = Article.create(:title => "foo2")

c1 = Category.create(:name => "cat1")
c2 = Category.create(:name => "cat2")
c3 = Category.create(:name => "cat3")

a1.categories = [Category.get(1), Category.get(2)]
a1.save

a1 = Article.get(1)
a1.categories = [Category.get(2), Category.get(3)]
a1.save

The last save fails:

INSERT INTO "articles_categories" ("article_id", "category_id") VALUES (1, 2)

error ~ columns article_id, category_id are not unique

I.e., it tries to insert the association with category 2 again, even though it already exists.

This is because the many-to-many proxy adds /new/ through_resources to the near_assocation.

Comments and changes to this ticket

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) January 8th, 2009 @ 05:20 AM

    • State changed from “new” to “accepted”
    • Assigned user changed from “Sam Smoot” to “Dan Kubb (dkubb)”
  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) May 28th, 2009 @ 03:19 AM

    • State changed from “accepted” to “resolved”

    I can confirm this is resolved in dm-core/next.

    @Lawrence: thank you very much for the patch but I ended up rewriting most of the Relationship code to fix a few many to many and SEL bugs.

    Please feel free to test out dm-core/next (it's very close to an RC release) and let me know if this is fixed or if you notice any other issues with it.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket

Attachments

Pages