sequelize - Cannot add foreign key constraint

后端 未结 2 817
既然无缘
既然无缘 2020-12-31 07:02

I\'m trying to set a 1:1 relation between two tables. RefreshToken table will have two foreignKey releated to Users table, as in this image:

I used sequelize-auto t

2条回答
  •  生来不讨喜
    2020-12-31 07:38

    I see two issues:

    No table should contain two primary keys and userId shouldn't be in integer it should be a UUID.

    I had a foreign key set to INT and it gave me error:

    Unhandled rejection SequelizeDatabaseError: foreign key constraint "constraint_name_here" cannot be implemented

    Try changing:

    userId: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      primaryKey: true,
      references: {
        model: 'Users',
        key: 'idUsers'
      }
    },
    

    To

    userId: {
      type: DataTypes.UUID,
      allowNull: false,
      foreignKey: true,
      references: {
        model: 'Users',
        key: 'idUsers'
      }
    },
    

提交回复
热议问题