Skip to content
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

Merged
merged 9 commits into from Sep 16, 2018

Conversation

lgaticaq
Copy link
Contributor

Pull Request check-list

Please make sure to review and check all of these items:

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

fix #9913

@codecov
Copy link

codecov bot commented Sep 12, 2018

Codecov Report

Merging #9914 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #9914      +/-   ##
==========================================
+ Coverage   96.04%   96.04%   +<.01%     
==========================================
  Files          63       63              
  Lines        9430     9433       +3     
==========================================
+ Hits         9057     9060       +3     
  Misses        373      373
Impacted Files Coverage Δ
lib/associations/belongs-to-many.js 97.03% <100%> (+0.02%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fefe374...32cf738. Read the comment docs.

});

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);
Copy link
Contributor

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() {
Copy link
Contributor

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(() => {
Copy link
Contributor

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

Copy link
Contributor

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

Copy link
Contributor

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@sushantdhiman sushantdhiman left a 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

@@ -522,6 +522,13 @@ User.findAll({
}]
});
```
Is possible rewrite the unique constraint name using **uniqueKey** option.
Copy link
Contributor

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(() => {
Copy link
Contributor

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(() => {

Project.belongsToMany(User, { through: UserProjects, uniqueKey: 'my_custom_unique' })
```

**Note:** _This option prevent error when the unique constraint name is too long._
Copy link
Contributor

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

@sushantdhiman sushantdhiman merged commit aa92764 into sequelize:master Sep 16, 2018
@sushantdhiman
Copy link
Contributor

Thanks @lgaticaq

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

Successfully merging this pull request may close these issues.

UNIQUE identifier name too long in belongsToMany
2 participants