-
-
Save lucasscariot/5b8747fbc8a6948a805c646fae4ceef8 to your computer and use it in GitHub Desktop.
| /* | |
| * Migration | |
| */ | |
| 'use strict'; | |
| module.exports = { | |
| up: function(queryInterface, Sequelize) { | |
| return queryInterface.createTable('Users', { | |
| firstName: { | |
| type: Sequelize.STRING | |
| }, | |
| lastName: { | |
| type: Sequelize.STRING | |
| }, | |
| email: { | |
| type: Sequelize.STRING | |
| }, | |
| createdAt: { | |
| allowNull: false, | |
| type: Sequelize.DATE | |
| }, | |
| updatedAt: { | |
| allowNull: false, | |
| type: Sequelize.DATE | |
| } | |
| }) | |
| .then(() => { | |
| return queryInterface.sequelize.query('ALTER TABLE "Users" ADD CONSTRAINT "username" PRIMARY KEY ("firstName", "lastName")'); | |
| }) | |
| }, | |
| down: function(queryInterface, Sequelize) { | |
| return queryInterface.dropTable('Users'); | |
| } | |
| }; | |
| /* | |
| * Model | |
| */ | |
| 'use strict'; | |
| module.exports = function(sequelize, DataTypes) { | |
| var User = sequelize.define('users', { | |
| firstName: { | |
| type: DataTypes.STRING, | |
| primaryKey: true, | |
| }, | |
| lastName: { | |
| type: DataTypes.STRING, | |
| primaryKey: true, | |
| }, | |
| email: DataTypes.STRING | |
| }); | |
| User.removeAttribute('id'); | |
| return User; | |
| }; | |
I did like @jmlsf mentioned, but had to add 'SET FOREIGN_KEY_CHECKS = 0'
ERROR: Cannot change column 'itemId': used in a foreign key constraint 'table_itemId_foreign_idx'
.then(() => queryInterface.sequelize.query('SET FOREIGN_KEY_CHECKS = 0'))
.then(() =>
queryInterface.addConstraint(
'table',
['itemId', 'item2Id'],
{
type: 'primary key',
name: 'name_ibfk_1'
}
)
)
.then(() => queryInterface.sequelize.query('SET FOREIGN_KEY_CHECKS = 1')),
@narayana1043
You've still got to use thewhereclause infindOne()
Something like thisfindOne({where:{composite_key_1:value})Let me know if this was helpful, Thanks.
When I create a composite key, by specifying primaryKey: true multiple times, am I able to find out the name of the composite key to use in the where clause?
Thanks
@ narayana1043
Todavía tienes que usar lawherecláusula enfindOne()
algo como estofindOne({where:{composite_key_1:value})
Hágame saber si esto fue útil, gracias.Cuando creo una clave compuesta, al especificar
primaryKey: truevarias veces, ¿puedo averiguar el nombre de la clave compuesta para usar en la cláusula where?Gracias
Hi!!! You could know how to get the name thanks
@narayana1043
You've still got to use the
whereclause infindOne()Something like this
findOne({where:{composite_key_1:value})Let me know if this was helpful, Thanks.