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

时间:2017-02-13 01:26:55

标签: 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,
        })

6 个答案:

答案 0 :(得分:26)

您可以参考此文档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']
        }
    ]
})

答案 1 :(得分:7)

  

我对将复合唯一约束应用于多个也有同样的问题   列,但没有任何工作与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']
      }
  }
});

答案 2 :(得分:3)

如果接受的不工作,请尝试以下代码。在我的情况下,它对我有用,而不是被接受的那个。

$array = [
    [1, 'dog', 'cat','sup'],
    [2, 'dog', 'fish', 'cow'],
    [3, 'dog', 'cat'],
    [4, 'monkey', 'rabbit', 'king', 'fish', 'duck']
];

$search = [ 'dog', 'cat'];
$size_search = count($search);
$store = [];

foreach ($array as $v) {
    if ( count(array_intersect($search, $v)) == $size_search )
        $store[] = $v[0];
}

print_r($store);

答案 3 :(得分:2)

我更喜欢使用具有复合唯一性的sequelize sync方法,如果不传递索引名称u,则会在向索引数组中添加许多索引时出现如下错误。

错误:SequelizeDatabaseError:标识符名称“ LONG_NAME”过长

module.exports = function (sequelize: any, DataTypes: any) {
return sequelize.define('muln_user_goals_transaction', {
    id: {
        type: DataTypes.INTEGER(11),
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
    },
    name: {
        type: DataTypes.STRING(),
        allowNull: false,
    },
    email: {
        type: DataTypes.STRING(),
        allowNull: false,
    },
    phone: {
        type: DataTypes.STRING(),
        allowNull: false,
    },
    amount: {
        type: DataTypes.INTEGER(8),
        allowNull: false
    },
    deleted: {
        type: DataTypes.BOOLEAN,
        defaultValue: false,
    },
}, {
    tableName: 'muln_user_goals_transaction',
    timestamps: false,
    indexes: [
        {
            name: 'unique_index',
            unique: true,
            fields: ['name', 'email', 'phone', 'amount', 'deleted']
        }
    ],
    defaultScope: {
        where: {
            deleted: false
        }
    }
});
};

答案 4 :(得分:1)

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

型号

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;
};

迁移

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");
  }
};

答案 5 :(得分:0)

序列化复合唯一(手动)

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Model', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      fieldOne: {
        type: Sequelize.INTEGER,
        unique: 'uniqueTag',
        allowNull: false,
        references: {
          model: 'Model1',
          key: 'id'
        },
        onUpdate: 'cascade',
        onDelete: 'cascade'
      },
      fieldsTwo: {
        type: Sequelize.INTEGER,
        unique: 'uniqueTag',
        allowNull: false,
        references: {
          model: 'Model2',
          key: 'id'
        },
        onUpdate: 'cascade',
        onDelete: 'cascade'
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    })
    .then(function() {
      return queryInterface.sequelize.query(
        'ALTER TABLE `UserFriends` ADD UNIQUE `unique_index`(`fieldOne`, `fieldTwo`)'
      );
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Model');
  }
};