Skip to content

Instantly share code, notes, and snippets.

@wdalmut
Forked from lucasscariot/model-user.js
Created May 7, 2018 12:05
Show Gist options
  • Save wdalmut/d0b46bb6c6fafff34c62cc0e4bffc300 to your computer and use it in GitHub Desktop.
Save wdalmut/d0b46bb6c6fafff34c62cc0e4bffc300 to your computer and use it in GitHub Desktop.
Composite Primary Key in Sequelize
/*
* 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