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
fix(associations): enable overwrite the name in unique constraint #9914
Conversation
Codecov Report
@@ Coverage Diff @@
## master #9914 +/- ##
==========================================
+ Coverage 96.04% 96.04% +<.01%
==========================================
Files 63 63
Lines 9430 9433 +3
==========================================
+ Hits 9057 9060 +3
Misses 373 373
Continue to review full report at Codecov.
|
}); | ||
|
||
return this.sequelize.sync({ force: true }).bind({}).then(() => { | ||
expect(this.Task.associations.MyUsers.through.model.rawAttributes.id_user_very_long_field.unique).to.have.lengthOf(24); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to check length
@@ -2235,6 +2235,65 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => { | |||
expect(ut2).to.have.length(1); | |||
}); | |||
}); | |||
|
|||
it('throw error with unique identifier very long', function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this test
uniqueKey: 'custom_user_group_unique' | ||
}); | ||
|
||
return this.sequelize.sync({ force: true }).bind({}).then(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No bind with arrow functions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use arrow functions I meant, get rid of bind
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return this.sequelize.sync({ force: true }).then(() => {
@@ -252,7 +252,12 @@ class BelongsToMany extends Association { | |||
if (this.primaryKeyDeleted === true) { | |||
targetAttribute.primaryKey = sourceAttribute.primaryKey = true; | |||
} else if (this.through.unique !== false) { | |||
const uniqueKey = [this.through.model.tableName, this.foreignKey, this.otherKey, 'unique'].join('_'); | |||
let uniqueKey; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add some info about this option in
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some minor changes and it can be merged
docs/associations.md
Outdated
@@ -522,6 +522,13 @@ User.findAll({ | |||
}] | |||
}); | |||
``` | |||
Is possible rewrite the unique constraint name using **uniqueKey** option. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Belongs-To-Many creates a unique key when primary key is not present on through
model. This unique key name can be overridden using uniqueKey option.
uniqueKey: 'custom_user_group_unique' | ||
}); | ||
|
||
return this.sequelize.sync({ force: true }).bind({}).then(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return this.sequelize.sync({ force: true }).then(() => {
docs/associations.md
Outdated
Project.belongsToMany(User, { through: UserProjects, uniqueKey: 'my_custom_unique' }) | ||
``` | ||
|
||
**Note:** _This option prevent error when the unique constraint name is too long._ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for this note
Thanks @lgaticaq |
Pull Request check-list
Please make sure to review and check all of these items:
npm run test
ornpm run test-DIALECT
pass with this change (including linting)?Description of change
fix #9913