Repository navigation
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.
no need to check length
| }); | ||
| }); | ||
|
|
||
| it('throw error with unique identifier very long', function() { |
| uniqueKey: 'custom_user_group_unique' | ||
| }); | ||
|
|
||
| return this.sequelize.sync({ force: true }).bind({}).then(() => { |
There was a problem hiding this comment.
No bind with arrow functions
There was a problem hiding this comment.
Use arrow functions I meant, get rid of bind
There was a problem hiding this comment.
return this.sequelize.sync({ force: true }).then(() => {
| 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.
Please add some info about this option in
- https://github.com/sequelize/sequelize/blob/master/docs/associations.md#belongs-to-many-associations
- https://github.com/sequelize/sequelize/blob/master/lib/model.js#L4258
sushantdhiman
left a comment
There was a problem hiding this comment.
Some minor changes and it can be merged
| }] | ||
| }); | ||
| ``` | ||
| Is possible rewrite the unique constraint name using **uniqueKey** option. |
There was a problem hiding this comment.
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.
return this.sequelize.sync({ force: true }).then(() => {
| 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.
No need for this note
|
Thanks @lgaticaq |
Pull Request check-list
Please make sure to review and check all of these items:
npm run testornpm run test-DIALECTpass with this change (including linting)?Description of change
fix #9913