Skip to content

fix(associations): enable overwrite the name in unique constraint - #9914

Merged
sushantdhiman merged 9 commits into
sequelize:masterfrom
eclass:bugfix/unique
Sep 16, 2018
Merged

sushantdhiman merged 9 commits into
sequelize:masterfrom
eclass:bugfix/unique

Conversation

@lgaticaq

Copy link
Copy Markdown
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

codecov Bot commented Sep 12, 2018 •

Copy link
Copy Markdown

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
Copy Markdown
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

});
});

it('throw error with unique identifier very long', function() {

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

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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@sushantdhiman sushantdhiman left a comment

Copy link
Copy Markdown
Contributor

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

Comment thread docs/associations.md Outdated
}]
});
```
Is possible rewrite the unique constraint name using **uniqueKey** option.

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

Comment thread 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._

Copy link
Copy Markdown
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
Copy Markdown
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