如何在插入数据时停止创建不需要的主键的续集?

时间:2017-02-17 22:16:14

标签: node.js postgresql sequelize.js

我有一个没有任何主键的续集模型:

module.exports = (sequelize, DataTypes) => {
  const usersDoors = sequelize.define('usersDoors',
    {
      user_uid: {
        type: DataTypes.UUID,
        allowNull: false,
      },
      door_uid: {
        type: DataTypes.UUID,
        allowNull: false,
      },
      property_manager_uid: {
        type: DataTypes.UUID,
        allowNull: true,
      },
      tenant_uid: {
        type: DataTypes.UUID,
        allowNull: true,
      },
      created_at: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
    },
    {
      tableName: 'users_doors',
      indexes: [
        {
          name: 'doors_users_indexes',
          unique: true,
          fields: ['user_uid', 'door_uid', 'property_manager_uid', 'tenant_uid'],
        },
      ],
      classMethods: {
        associate: (models) => {
          usersDoors.belongsTo(models.users, { foreignKey: 'user_uid' });
          usersDoors.belongsTo(models.doors, { foreignKey: 'door_uid' });
          usersDoors.belongsTo(models.propertyManagers, { foreignKey: 'property_manager_uid' });
          usersDoors.belongsTo(models.tenants, { foreignKey: 'tenant_uid' });
        },
      },
    });

  return usersDoors;
};

当我插入数据时(通过sequelize),sequelize会向表中添加一个复合主键,其中包含前两列(user_uid和door_uid)。这打破了我的东西,我不想要它。

如何在插入数据时停止创建不需要的主键的续集?

更多详情:

  • 方言:postgresql
  • sequelize(节点包)版本:3.30.2
  • postgresql版本:psql(9.6.1,服务器9.5.5)
  • 节点版本(这与任何事情无关):6.9.1

最后,这是此模型的续集迁移:

const tableName = 'users_doors';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable(tableName, {
      user_uid: {
        type: Sequelize.UUID,
        allowNull: false,
        references: {
          model: 'users',
          key: 'uid',
        },
      },
      door_uid: {
        type: Sequelize.UUID,
        allowNull: false,
        references: {
          model: 'doors',
          key: 'uid',
        },
      },
      property_manager_uid: {
        type: Sequelize.UUID,
        allowNull: true,
        references: {
          model: 'property_managers',
          key: 'uid',
        },
      },
      tenant_uid: {
        type: Sequelize.UUID,
        allowNull: true,
        references: {
          model: 'tenants',
          key: 'uid',
        },
      },
      created_at: {
        type: Sequelize.INTEGER,
        allowNull: false,
      },
    })
      .then(() => {
        return queryInterface.addIndex('users_doors',
          ['user_uid', 'door_uid', 'property_manager_uid', 'tenant_uid'],
          {
            indexName: 'doors_users_indexes',
            indicesType: 'UNIQUE',
          });
      });
  },

  down: (queryInterface) => {
    return queryInterface.dropTable(tableName)
      .then(() => {
        return queryInterface.removeIndex('users_doors', ['user_uid', 'door_uid', 'property_manager_uid', 'tenant_uid']);
      });
  },
};

1 个答案:

答案 0 :(得分:2)

您只需要为续集模型添加属性,以及将作为主键的迁移

// in sequelize model definition
const usersDoors = sequelize.define('usersDoors',
{
  id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
  },
  user_uid: {
      type: DataTypes.UUID,
      allowNull: false,
  },
  // other fields

// in the migrations file
return queryInterface.createTable(tableName, {
  id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
  },
  user_uid: {
      type: Sequelize.UUID,
      allowNull: false,
      references: {
          model: 'users',
          key: 'uid',
      },
  },
  // other fields

根据文档,当您创建M:M关系时,sequelize会自动创建一个主键,该主键由两个引用表创建关系的表的外键组成(usersdoors在您的情况下)

  

该表将由两个表的键组合唯一标识

修改

假设您在belongsToManyusers模型上使用了doors,则可以避免在unique index上自动创建user_uiddoor_uid。您需要在m:m关系初始化的unique: false对象中指定through条件(在两个模型中都要避免任何自动唯一约束)

// in users model
users.belongsToMany(
    models.doors,
    {
        through: { model: models.usersDoors, unique: false },
        foreignKey: 'user_uid'
    }
);

// in doors model
doors.belongsToMany(
    models.users,
    {
        through: { model: models.usersDoors, unique: false },
        foreignKey: 'door_uid'
    }
);