0

i am trying to migrate the dataset in mysql database being the role_id and module_id same mysql is refusing to store saying

sqlMessage: "Duplicate entry '3-1' for key 'role_module_bridge.role_module_bridge_module_id_role_id_unique'", sql: "INSERT INTO role_module_bridge (id,module_id,role_id,permission_id,createdAt,updatedAt) VALUES (NULL,3,1,2,'2025-09-15 19:19:37','2025-09-15 19:19:37'),(NULL,3,1,3,'2025-09-15 19:19:37','2025-09-15 19:19:37');",

the sample data

[
  { role_id: 1, module_id: 3, permission_id: 2 },
  { role_id: 1, module_id: 3, permission_id: 3 }
]
async function grant(
  roleName: string,
  moduleNames: string[],
  permissionNames: string[]
) {
  const { Role, Modules, Permission, RoleModuleBridge } = await import(
    '../src/models/index'
  );

  const role = await Role.findOne({ where: { role: roleName }, raw: true });
  const mods = await Modules.findAll({
    where: { module_name: moduleNames } as any,
    raw: true,
  });

  const perms = await Permission.findAll({
    where: { permission_name: permissionNames } as any,
    raw: true,
  });

  console.log("role: ", role);
  console.log("module: ", mods);
  console.log("permission: ", perms);
  console.log(permissionNames);

  const rows = [];
  for (const m of mods) {
    for (const p of perms) {
      rows.push({
        role_id: role?.id,
        module_id: m?.id,
        permission_id: p?.id,
      });
    }
  }

  console.log(rows);
  await RoleModuleBridge.bulkCreate(rows);
}

RoleModuleBridge Model

import sequelize from '@/lib/sequelize';
import { Model, Optional, DataTypes } from 'sequelize';

export interface RoleModulesBridgeAttributes {
  id: number;
  module_id: number;
  role_id: number;
  permission_id: number;
}

export interface RoleModulesBridgeCreationAttributes
  extends Optional<RoleModulesBridgeAttributes, 'id'> {}

const RoleModuleBridge = sequelize.define<
  Model<RoleModulesBridgeAttributes, RoleModulesBridgeCreationAttributes>
>(
  'RoleModuleBridge',
  {
    id: {
      type: DataTypes.INTEGER.UNSIGNED,
      autoIncrement: true,
      primaryKey: true,
    },
    module_id: {
      type: DataTypes.INTEGER.UNSIGNED,
      allowNull: false,
      references: {
        model: 'modules',
        key: 'id',
      },
      onUpdate: 'CASCADE',
      onDelete: 'RESTRICT',
    },
    role_id: {
      type: DataTypes.INTEGER.UNSIGNED,
      allowNull: false,
      references: {
        model: 'roles',
        key: 'id',
      },
      onUpdate: 'CASCADE',
      onDelete: 'RESTRICT',
    },
    permission_id: {
      type: DataTypes.INTEGER.UNSIGNED,
      allowNull: false,
      references: {
        model: 'permissions',
        key: 'id',
      },
      onUpdate: 'CASCADE',
      onDelete: 'RESTRICT',
    },
  },
  {
    tableName: 'role_module_bridge',
    paranoid: true,
    indexes: [
      {
        unique: true,
        fields: ['module_id', 'role_id', 'permission_id'], // All three fields for uniqueness
        name: 'role_module_bridge_unique_index',
      },
    ],
  }
);


export default RoleModuleBridge;

we have modules for every crud operations and which roles will be checked from the database wheter the user is allowed to access the specific module and what what changes the user can do based on the permission

[
  { role_id: 1, module_id: 3, permission_id: 2 },
  { role_id: 1, module_id: 3, permission_id: 3 }
]

the above error states below information


[
  { role_id: 1("teacher"), module_id: 3("classes"), permission_id: 2("read") },
  { role_id: 1("teacher"), module_id: 3("classes"), permission_id: 3("update") }
]

tried steps

  1. i tried Modifying my Sequelize model to use a composite unique constraint on (module_id, role_id, permission_id) but didn't worked at all

  2. i tried both bulkCreate() and individual create() calls by using loop but same problem

2
  • Please add the DDL for role_module_bridge.role_module_bridge_module_id_role_id_unique from your DB. Also I don't see a way how you synchronize your changes in models with DB tables and FKs Commented Sep 27 at 17:01
  • It seems that in your database you have an old unique index role_module_bridge_module_id_role_id_unique - containing only rule and module. Recreating the table should help. Commented Sep 28 at 14:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.