如何在sequelize中定义多列的唯一索引

lbo*_*yel 20 mysql sequelize.js

如何在sequelize中的列组合上定义唯一索引.例如,我想在user_id,count和name上添加唯一索引.

var Tag = sequelize.define('Tag', {
        id: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        user_id: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
        },
        count: {
            type: DataTypes.INTEGER(11),
            allowNull: true
        },
        name: {
            type: DataTypes.STRING,
            allowNull: true,
        })
Run Code Online (Sandbox Code Playgroud)

小智 39

你可以参考这个文档http://docs.sequelizejs.com/en/latest/docs/models-definition/#indexes

您将需要更改您的定义,如下所示并呼叫同步

var Tag = sequelize.define('Tag', {
    id: {
        type: DataTypes.INTEGER(11),
        allowNull: false,
        primaryKey: true,
        autoIncrement: true
    },
    user_id: {
        type: DataTypes.INTEGER(11),
        allowNull: false,
    },
    count: {
        type: DataTypes.INTEGER(11),
        allowNull: true
    },
    name: {
        type: DataTypes.STRING,
        allowNull: true,
    }
},
{
    indexes: [
        {
            unique: true,
            fields: ['user_id', 'count', 'name']
        }
    ]
});
Run Code Online (Sandbox Code Playgroud)

  • 这对我不起作用。以下任何答案都没有。我正在尝试运行与此等效的查询:`CREATE TABLE score (id INT PRIMARY KEY NOT NULL, oppId INT NOT NULL, oppType TEXT NOT NULL, tree TEXT NOT NULL, version INT NOT NULL, score JSON NOT NULL, CONSTRAINT uniq_scores UNIQUE(oppId, oppType, tree, version));`。当我将唯一索引与 sequelize 一起使用时,我仍然能够在这些列中创建具有相同值的两行。还有其他人有这些问题吗? (11认同)

Sun*_*S.M 18

我对将复合唯一约束应用于多个列有相同的问题,但最终无法通过以下代码修复Mysql,Sequelize(4.10.2)和NodeJs 8.9.4的问题。

queryInterface.createTable('actions', {
  id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
  },
  system_id: {
      type: Sequelize.STRING,
      unique: 'actions_unique',
  },
  rule_id: {
      type: Sequelize.STRING,
      unique: 'actions_unique',
  },
  plan_id: {
      type: Sequelize.INTEGER,
      unique: 'actions_unique',
  }
}, {
  uniqueKeys: {
      actions_unique: {
          fields: ['system_id', 'rule_id', 'plan_id']
      }
  }
});
Run Code Online (Sandbox Code Playgroud)

  • 这应该是公认的答案。调用同步不利于保持迁移轨迹。 (6认同)
  • 另外,对我来说,只需提及“unique: 'actions_unique'”对于我想要在复合唯一规则中起作用的列。没有添加 `uniqueKeys` 选项。 (2认同)

M.A*_*pon 7

如果接受的代码不起作用,请尝试以下代码。就我而言,它为我工作,而不是被接受的工作。

var Tag = sequelize.define('Tag', {
    id: {
        type: DataTypes.INTEGER(11),
        allowNull: false,
        primaryKey: true,
        autoIncrement: true
    },
    user_id: {
        type: DataTypes.INTEGER(11),
        allowNull: false,
        unique: 'uniqueTag',
    },
    count: {
        type: DataTypes.INTEGER(11),
        allowNull: true,
        unique: 'uniqueTag',
    },
    name: {
        type: DataTypes.STRING,
        allowNull: true,
        unique: 'uniqueTag',
    }
});
Run Code Online (Sandbox Code Playgroud)


San*_*jan 6

我试图在单列上创建索引。这对我有用。希望这可以帮助。

模型

module.exports = (sequelize, DataTypes) => {
  const Tag = sequelize.define(
    "Tag",
    {
      name: { type: DataTypes.STRING, unique: true },
      nVideos: DataTypes.INTEGER
    },
    {
      indexes: [
        {
          unique: true,
          fields: ["name"]
        }
      ]
    }
  );

  return Tag;
};
Run Code Online (Sandbox Code Playgroud)

移民

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable(
      "Tags",
      {
        id: {
          allowNull: false,
          autoIncrement: true,
          primaryKey: true,
          type: Sequelize.INTEGER
        },
        name: {
          type: Sequelize.STRING,
          unique: "unique_tag"
        },
        nVideos: { type: Sequelize.INTEGER },
        createdAt: {
          allowNull: false,
          type: Sequelize.DATE
        },
        updatedAt: {
          allowNull: false,
          type: Sequelize.DATE
        }
      },
      {
        uniqueKeys: {
          unique_tag: {
            customIndex: true,
            fields: ["name"]
          }
        }
      }
    );
  },
  down: queryInterface => {
    return queryInterface.dropTable("Tags");
  }
};

Run Code Online (Sandbox Code Playgroud)