diff --git a/src/database/migrations/20240401123752-make-user-id-primary-key-in-user-sessions-table.js b/src/database/migrations/20240401123752-make-user-id-primary-key-in-user-sessions-table.js new file mode 100644 index 000000000..6786b39a9 --- /dev/null +++ b/src/database/migrations/20240401123752-make-user-id-primary-key-in-user-sessions-table.js @@ -0,0 +1,27 @@ +'use strict' + +module.exports = { + async up(queryInterface, Sequelize) { + // Remove the primary key constraint from 'id' + await queryInterface.removeConstraint('user_sessions', 'user_sessions_pkey') + + // Add a unique constraint for the combination of 'id' and 'user_id' + await queryInterface.addConstraint('user_sessions', { + fields: ['id', 'user_id'], + type: 'primary key', + name: 'user_sessions_pkey', + }) + }, + + async down(queryInterface, Sequelize) { + // Remove the unique constraint for the combination of 'id' and 'user_id' + await queryInterface.removeConstraint('user_sessions', 'user_sessions_pkey') + + // Add back the primary key constraint on 'id' + await queryInterface.addConstraint('user_sessions', { + type: 'primary key', + fields: ['id'], + name: 'user_sessions_pkey', + }) + }, +} diff --git a/src/database/models/user-sessions.js b/src/database/models/user-sessions.js index ef7d0f957..7b961e137 100644 --- a/src/database/models/user-sessions.js +++ b/src/database/models/user-sessions.js @@ -12,6 +12,7 @@ module.exports = (sequelize, DataTypes) => { user_id: { type: DataTypes.INTEGER, allowNull: false, + primaryKey: true, }, started_at: { type: DataTypes.BIGINT,