New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Keep getting unique validation error: How to allow duplicate entries in N:M (belongsToMany)? #3220
Comments
unique entries in N:M is currently not supported. You can use |
Can you explain further with what you mean "so you manually have to create entries". Right now to create the history, I just do:
Which triggers the unique validation errors if a duplicate of Do you mean I should remove my
|
Ah well that's great, that's what you have to do.
(the unique: false prop is on the relation, not on the foreign key option) |
Hm, I'm still getting the unique validation errors for both
|
@maplesap what does the create table sql look like? |
This is the output:
|
@maplesap that's not the create table statement though :) |
Okay! I created a small version with the problem. Here is the entire code: app.js
models/user.js
models/item.js
models/history.js
index.html
Here is the output when I first run the app:
When I insert a duplicate combination of
|
For the moment I kind of cheated and created history Which won't cause the unique validation error to occur and I can set my ids. It seems like setting Is returning true and |
I'm seeing this same issue and have created this short gist where I was able to replicate the problem using some examples from the docs. |
Forgive me if this a noob thing to say, I'm new to Sequelize and am a little rusty on SQL, but it looks like when you create
It's using the For example, purchasing/viewing the same product multiple times. To get around this I've created a separate Don't know if this is the best solution but it certainly works. |
@chrisjhoughton I'm afraid we have very poor support for non-unique N:M relations at the time. |
@mickhansen no problem, the workaround above is working great! |
@chrisjhoughton var Doctor = sequelize.define('doctor', {
name: Sequelize.STRING
});
var Patient = sequelize.define('patient', {
name: Sequelize.STRING
});
var Appointment = sequelize.define('appointment', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
date: Sequelize.DATE,
paymentType: Sequelize.STRING
});
Doctor.belongsToMany(Patient, {through: Appointment, unique: false, foreignKey: {name: "patientId", unique: false}});
Patient.belongsToMany(Doctor, {through: Appointment, unique: false, foreignKey: {name: "doctorId", unique: false}}); Even still, it creates:
I realize some of those |
@j7caiman In your last two lines, try following instead
|
This should be mentioned in docs very explicitly. |
@zachguo: Unless there's something in the Secondly, I wasn't able to find the information you suggested in the docs anywhere. Here's where I looked: http://sequelize.readthedocs.org/en/latest/api/associations/belongs-to-many/ |
I met the same thing: unique: false does not prevents creation of unique index. First I came to google and found this post. And I've tried as @mickhansen said:
But this is not correct. After deep debugging I've found that belongsToMany expects the complex object 'through' if we want to set additional options to the relation. So the correct code is:
As I've found this, I search the sequel docs for word 'unique'. And found this feature in n:m chapter: See the example for Post:
P.S. Maybe this options may be covered in manual more clearly. I've read man several times, but 'unique: false' feature was discovered only with Google. |
Hmm, my mistake then, ideally it should work simply as an option on the relation though. |
Any update on this issue? |
Im also encountering this issue. seems like unique: false, both in through and outside it do nothing. Digging into the library, i THINK, the code doesnt take into account when this.through.unique is false. It checks for everything BUT false. I was looking at the associations/belongs-to-many.js file. |
after tried @statyan solution of |
- There's a bug in Sequelize "BelongsToMany" where it refuses to accept multiple foreign keys ("user_id and photo_id must be unique"), but this is not correct error for a join table because the fields all of their own unique IDs. We found a Github thread where people discovered this same bug, but it hasn't been fixed by the Sequelize team: sequelize/sequelize#3220 - Amad and Tony
- There's a bug in Sequelize "BelongsToMany" where it refuses to accept multiple foreign keys ("user_id and photo_id must be unique"), but this is not correct error for a join table because the fields all of their own unique IDs. We found a Github thread where people discovered this same bug, but it hasn't been fixed by the Sequelize team: sequelize/sequelize#3220 - Amad and Tony
This comment has been minimized.
This comment has been minimized.
Hi, is this still ongoing? I need the relation table to avoid having a composite primary key with the 2 relation fields, is that possible? Perhaps we can give it the field name for the primary key? Shall I need to create my own model for this and then set unique: false on the BelongsToMany ? |
The unique constraint error deletes the previous records too. This isn't expected. Please resolve the issue |
I'm also running into this issue |
I also have this issue |
+1 |
1 similar comment
+1 |
For anyone with this issue, here is a suggested workaround: // Instead of using a Many-to-Many relationship:
A.belongsToMany(B, { through: C });
B.belongsToMany(C, { through: C });
// Throw away the Many-to-Many relationship altogether and treat C as a full-fledged standard model
A.hasMany(C);
C.belongsTo(A);
B.hasMany(C);
C.belongsTo(B); However the solution above of course will change how you fundamentally perform includes and the like. // This way, instead of
A.findAll({
include: B
});
// You will have to do
A.findAll({
include: {
model: C
include: B
}
})
// and the structure of the query result will be a bit different but all the content you need will be there |
Although the workaround above is a drastic conceptual change and would take a lot of effort to convert from what should be working, since it's not working I doubt anyone will really have lots of code to change. Hopefully the above is a workaround that helps everyone. Let me know if it doesn't work for any of you |
This code will help you. Look at this:
Main thing there primaryKey must be false |
User.belongsToMany(Room, { through: { model: Chat, unique: false },onDelete:"CASCADE" }); zis works for me |
It doesn't work for me at all, all the suggestions never worked i used hasmany() |
The unique validation check is preventing me from creating duplicate entries in my join table:
When a user buys the same item again, resulting in duplicate combinations of
itemId
anduserId
inhistory
table, sequelize will throw validation errors:But I have set
unique: false
on the foreignkeys, and have created a primaryKey forhistory
table, so this validation error confused me. What's the proper way to allow duplicate entries in N:M?The text was updated successfully, but these errors were encountered: