Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit aa92764f
authored
Sep 16, 2018
by
Leonardo Gatica
Committed by
Sushant
Sep 16, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(belongsToMany): override unique constraint name with uniqueKey (#9914)
1 parent
ba12835b
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
104 additions
and
2 deletions
docs/associations.md
lib/associations/belongs-to-many.js
lib/model.js
test/integration/associations/belongs-to-many.test.js
test/unit/associations/belongs-to-many.test.js
docs/associations.md
View file @
aa92764
...
...
@@ -522,6 +522,11 @@ User.findAll({
}]
});
```
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.
```
js
Project
.
belongsToMany
(
User
,
{
through
:
UserProjects
,
uniqueKey
:
'my_custom_unique'
})
```
## Naming strategy
...
...
lib/associations/belongs-to-many.js
View file @
aa92764
...
...
@@ -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
;
if
(
typeof
this
.
options
.
uniqueKey
===
'string'
&&
this
.
options
.
uniqueKey
!==
''
)
{
uniqueKey
=
this
.
options
.
uniqueKey
;
}
else
{
uniqueKey
=
[
this
.
through
.
model
.
tableName
,
this
.
foreignKey
,
this
.
otherKey
,
'unique'
].
join
(
'_'
);
}
targetAttribute
.
unique
=
sourceAttribute
.
unique
=
uniqueKey
;
}
...
...
lib/model.js
View file @
aa92764
...
...
@@ -3274,7 +3274,7 @@ class Model {
setDataValue
(
key
,
value
)
{
const
originalValue
=
this
.
_previousDataValues
[
key
];
if
(
(
!
Utils
.
isPrimitive
(
value
)
&&
value
!==
null
)
||
value
!==
originalValue
)
{
if
(
!
Utils
.
isPrimitive
(
value
)
&&
value
!==
null
||
value
!==
originalValue
)
{
this
.
changed
(
key
,
true
);
}
...
...
@@ -4269,6 +4269,7 @@ class Model {
* @param {string} [options.onDelete='SET NULL|CASCADE'] SET NULL if foreignKey allows nulls, CASCADE if otherwise
* @param {string} [options.onUpdate='CASCADE'] Sets 'ON UPDATE'
* @param {boolean} [options.constraints=true] Should on update and on delete constraints be enabled on the foreign key.
* @param {string} [options.uniqueKey] The custom name for unique constraint.
*
* @returns {HasOne}
*
...
...
test/integration/associations/belongs-to-many.test.js
View file @
aa92764
...
...
@@ -2235,6 +2235,35 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {
expect
(
ut2
).
to
.
have
.
length
(
1
);
});
});
it
(
'create custom unique identifier'
,
function
()
{
this
.
UserTasksLong
=
this
.
sequelize
.
define
(
'table_user_task_with_very_long_name'
,
{
id_user_very_long_field
:
{
type
:
DataTypes
.
INTEGER
(
1
)
},
id_task_very_long_field
:
{
type
:
DataTypes
.
INTEGER
(
1
)
}
},
{
tableName
:
'table_user_task_with_very_long_name'
}
);
this
.
User
.
belongsToMany
(
this
.
Task
,
{
as
:
'MyTasks'
,
through
:
this
.
UserTasksLong
,
foreignKey
:
'id_user_very_long_field'
});
this
.
Task
.
belongsToMany
(
this
.
User
,
{
as
:
'MyUsers'
,
through
:
this
.
UserTasksLong
,
foreignKey
:
'id_task_very_long_field'
,
uniqueKey
:
'custom_user_group_unique'
});
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(()
=>
{
expect
(
this
.
Task
.
associations
.
MyUsers
.
through
.
model
.
rawAttributes
.
id_user_very_long_field
.
unique
).
to
.
equal
(
'custom_user_group_unique'
);
expect
(
this
.
Task
.
associations
.
MyUsers
.
through
.
model
.
rawAttributes
.
id_task_very_long_field
.
unique
).
to
.
equal
(
'custom_user_group_unique'
);
});
});
});
describe
(
'Association options'
,
()
=>
{
...
...
test/unit/associations/belongs-to-many.test.js
View file @
aa92764
...
...
@@ -593,6 +593,68 @@ describe(Support.getTestDialectTeaser('belongsToMany'), () => {
expect
(
Group
.
associations
.
MyUsers
.
through
.
model
.
rawAttributes
.
GroupId
.
onUpdate
).
to
.
equal
(
'SET NULL'
);
expect
(
Group
.
associations
.
MyUsers
.
through
.
model
.
rawAttributes
.
GroupId
.
onDelete
).
to
.
equal
(
'RESTRICT'
);
});
it
(
'generate unique identifier with very long length'
,
function
()
{
const
User
=
this
.
sequelize
.
define
(
'User'
,
{},
{
tableName
:
'table_user_with_very_long_name'
}),
Group
=
this
.
sequelize
.
define
(
'Group'
,
{},
{
tableName
:
'table_group_with_very_long_name'
}),
UserGroup
=
this
.
sequelize
.
define
(
'GroupUser'
,
{
id_user_very_long_field
:
{
type
:
DataTypes
.
INTEGER
(
1
)
},
id_group_very_long_field
:
{
type
:
DataTypes
.
INTEGER
(
1
)
}
},
{
tableName
:
'table_user_group_with_very_long_name'
}
);
User
.
belongsToMany
(
Group
,
{
as
:
'MyGroups'
,
through
:
UserGroup
,
foreignKey
:
'id_user_very_long_field'
});
Group
.
belongsToMany
(
User
,
{
as
:
'MyUsers'
,
through
:
UserGroup
,
foreignKey
:
'id_group_very_long_field'
});
expect
(
Group
.
associations
.
MyUsers
.
through
.
model
===
User
.
associations
.
MyGroups
.
through
.
model
);
expect
(
Group
.
associations
.
MyUsers
.
through
.
model
.
rawAttributes
.
id_user_very_long_field
.
unique
).
to
.
have
.
lengthOf
(
92
);
expect
(
Group
.
associations
.
MyUsers
.
through
.
model
.
rawAttributes
.
id_group_very_long_field
.
unique
).
to
.
have
.
lengthOf
(
92
);
expect
(
Group
.
associations
.
MyUsers
.
through
.
model
.
rawAttributes
.
id_user_very_long_field
.
unique
).
to
.
equal
(
'table_user_group_with_very_long_name_id_group_very_long_field_id_user_very_long_field_unique'
);
expect
(
Group
.
associations
.
MyUsers
.
through
.
model
.
rawAttributes
.
id_group_very_long_field
.
unique
).
to
.
equal
(
'table_user_group_with_very_long_name_id_group_very_long_field_id_user_very_long_field_unique'
);
});
it
(
'generate unique identifier with custom name'
,
function
()
{
const
User
=
this
.
sequelize
.
define
(
'User'
,
{},
{
tableName
:
'table_user_with_very_long_name'
}),
Group
=
this
.
sequelize
.
define
(
'Group'
,
{},
{
tableName
:
'table_group_with_very_long_name'
}),
UserGroup
=
this
.
sequelize
.
define
(
'GroupUser'
,
{
id_user_very_long_field
:
{
type
:
DataTypes
.
INTEGER
(
1
)
},
id_group_very_long_field
:
{
type
:
DataTypes
.
INTEGER
(
1
)
}
},
{
tableName
:
'table_user_group_with_very_long_name'
}
);
User
.
belongsToMany
(
Group
,
{
as
:
'MyGroups'
,
through
:
UserGroup
,
foreignKey
:
'id_user_very_long_field'
,
uniqueKey
:
'custom_user_group_unique'
});
Group
.
belongsToMany
(
User
,
{
as
:
'MyUsers'
,
through
:
UserGroup
,
foreignKey
:
'id_group_very_long_field'
,
uniqueKey
:
'custom_user_group_unique'
});
expect
(
Group
.
associations
.
MyUsers
.
through
.
model
===
User
.
associations
.
MyGroups
.
through
.
model
);
expect
(
Group
.
associations
.
MyUsers
.
through
.
model
.
rawAttributes
.
id_user_very_long_field
.
unique
).
to
.
have
.
lengthOf
(
24
);
expect
(
Group
.
associations
.
MyUsers
.
through
.
model
.
rawAttributes
.
id_group_very_long_field
.
unique
).
to
.
have
.
lengthOf
(
24
);
expect
(
Group
.
associations
.
MyUsers
.
through
.
model
.
rawAttributes
.
id_user_very_long_field
.
unique
).
to
.
equal
(
'custom_user_group_unique'
);
expect
(
Group
.
associations
.
MyUsers
.
through
.
model
.
rawAttributes
.
id_group_very_long_field
.
unique
).
to
.
equal
(
'custom_user_group_unique'
);
});
});
describe
(
'association hooks'
,
()
=>
{
beforeEach
(
function
()
{
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment