Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

"duplicate key value violates unique constraint" on updating a record which is part on an M-to-M relationship #243

Closed
gearoidoceallaigh opened this issue Mar 6, 2013 · 1 comment

Comments

@gearoidoceallaigh
Copy link

Hi,

This error has bugged me for a while, I've done lots of searching and asked regarding it on Stackoverflow (http://stackoverflow.com/questions/13952050/why-is-datamapper-trying-to-re-save-my-relationships) with no solution forthcoming so said I'd raise it here.

Basically I have a class User defined as follows:

class User    
  include DataMapper::Resource

  has n, :interests, :through => Resource, :constraint => :skip  
end

and a model interests defined as:

class Interest
  include DataMapper::Resource

  has n, :users, :through => Resource, :constraint => :skip
end

This creates a link table with two columns which make up a composite key. I can save new records fine but when I try to update a user like so:

user.update(params[:user]) 

I get the following error:

 duplicate key value violates unique constraint "user_interests_pkey"
 DETAIL:  Key (user_id, interest_id)=(5, 1) already exists.

Would really appreciate anyone who can shed any light on this - its baffled me!

@gearoidoceallaigh
Copy link
Author

FWIW this comment from solnic on the dm-rails repo explains the problem:

datamapper/dm-rails#9 (comment)

He suggests avoiding using the autogenerated link tables in datamapper.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant