未处理的拒绝sequelizedatabaseerror:无法添加外键约束

uqdfh47h  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(267)

我有两个表:admin\u user和admin\u account。

const Sequelize = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  var admin_user = sequelize.define('admin_user', {
    id: {
      autoIncrement: true,
      type: Sequelize.INTEGER,
      primaryKey: true
    },
    name: {
      type: Sequelize.STRING
    },
    email: {
      type: Sequelize.STRING,
      allowNull: false,
      unique: true,
      validate: {
        isEmail: true,
      }
    },
    user_name:{
      type: Sequelize.STRING,
      allowNull: false,
      unique: true
    }
  });

  admin_user.associate = (models) =>{
    admin_user.hasOne(models.admin_account, { foreignKey: 'admin_user_id' });
  };

  return admin_user;
};

'use strict';
const Sequelize = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  var admin_account = sequelize.define('admin_account', {
    admin_user_id:{
      type: Sequelize.INTEGER,
      primaryKey: true
    },
    password: {
      type: Sequelize.STRING
    }
  });
  return admin_account;
};

创建表时出现此错误:
执行(默认):如果不存在,则创建表 admin_users ( id 整数自动增量, name 瓦尔查尔(255), email varchar(255)非空唯一, user_name varchar(255)非空唯一, createdAt 日期时间不为空, updatedAt datetime不为空,主键( id ))引擎=innodb;执行(默认):显示索引自 admin_usersmilkman_prod1 执行(默认):如果不存在,则创建表 admin_accounts ( admin_user_id 整数, password 瓦尔查尔(255), createdAt 日期时间不为空, updatedAt datetime不为空,主键( admin_user_id ),外键( admin_user_id )参考文献 admin_users ( id )在delete上设置null在update cascade上)engine=innodb;未处理的拒绝sequelizedatabaseerror:无法添加外键约束

cwtwac6a

cwtwac6a1#

请尝试使用此代码:

'use strict';
    const Sequelize = require('sequelize');
    module.exports = (sequelize, DataTypes) => {
      var admin_account = sequelize.define('admin_user', {
        admin_user_id:{
          type: Sequelize.INTEGER,
          primaryKey: true
        },
        password: {
          type: Sequelize.STRING
        }
      });
admin_account.associate = (models) =>{
    admin_account.belongsTo(models.admin_user);
  };
      return admin_account;
    };

相关问题