Last active
June 22, 2023 17:08
-
-
Save lucasscariot/5b8747fbc8a6948a805c646fae4ceef8 to your computer and use it in GitHub Desktop.
Composite Primary Key in Sequelize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi!!! You could know how to get the name thanks