Sequelize sync()+ foregin key,抛出错误

时间:2017-08-09 11:31:16

标签: javascript node.js express sequelize.js

我有三个模型,user.model.js,user-log.model.js和address.model.js

它在我的数据库中创建表,但是这里user_logs和地址具有用户表的foregin键 每次,当我删除所有表并运行节点服务器时,它会抛出此错误

代码:

user.model.js

var User = db.sequelize.define( 'user' , {
    ......
});

module.exports = User;

用户-log.model.js

var User = require('./user.model');

var Userlog = db.sequelize.define( 'user_log' , {
    user_id : {
        type: db.Sequelize.INTEGER,
    }
    ........
);


Userlog.belongsTo(User,{foreignKey: 'user_id'});
User.hasMany(Userlog,{foreignKey: 'user_id'});

Userlog.sync();

module.exports = Userlog;

address.model.js

var User = require('./user.model');

var Address = db.sequelize.define('address' , {
    user_id : {
        type : db.Sequelize.INTEGER,
    }
    ......
);

Address.belongsTo( User , {foreignKey: 'user_id'} );
User.hasMany( Address , {foreignKey: 'user_id'} );

Address.sync();

module.exports = Address;

错误:

[nodemon] starting `node server.js`
Executing (default): SELECT 1+1 AS result
Executing (default): CREATE TABLE IF NOT EXISTS `users` (`id` INTEGER NOT NULL auto_increment , `firstname` VARCHAR(255) NOT NULL, `lastname` VARCHAR(255) NOT NULL, `email_id` VARCHAR(255) NOT NULL UNIQUE, `fb_id` VARCHAR(255), `password` VARCHAR(255), `comapany_id` INTEGER DEFAULT 0, `user_type` INTEGER DEFAULT 2, `anonymous` TINYINT(1) DEFAULT false, `admin_anonymous` TINYINT(1) DEFAULT false, `status` TINYINT(1) DEFAULT false, `created_at` DATETIME NOT NULL, `updated_at` DATETIME NOT NULL, UNIQUE `users_email_id_unique` (`email_id`), PRIMARY KEY (`id`)) ENGINE=InnoDB;
Executing (default): CREATE TABLE IF NOT EXISTS `user_logs` (`id` INTEGER NOT NULL auto_increment , `user_id` INTEGER, `platform` INTEGER DEFAULT 0, `login_time` DATETIME DEFAULT '2017-08-09 11:06:47', PRIMARY KEY (`id`), FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE) ENGINE=InnoDB;
Executing (default): CREATE TABLE IF NOT EXISTS `addresses` (`id` INTEGER NOT NULL auto_increment , `user_id` INTEGER, `line_1` VARCHAR(255), `line_2` VARCHAR(255), `line_3` VARCHAR(255), `city` VARCHAR(255), `state` VARCHAR(255), `country` VARCHAR(255), `zipcode` VARCHAR(255), `lat` VARCHAR(255), `lang` VARCHAR(255), `created_at` DATETIME NOT NULL, `updated_at` DATETIME NOT NULL, PRIMARY KEY (`id`), FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE) ENGINE=InnoDB;
Connection has been established successfully.
Unhandled rejection SequelizeDatabaseError: Cannot add foreign key constraint
    at Query.formatError (/var/opt/roundTable/node_modules/sequelize/lib/dialects/mysql/query.js:222:16)
    at Query.connection.query [as onResult] (/var/opt/roundTable/node_modules/sequelize/lib/dialects/mysql/query.js:55:23)
    at Query.Command.execute (/var/opt/roundTable/node_modules/mysql2/lib/commands/command.js:30:12)
    at Connection.handlePacket (/var/opt/roundTable/node_modules/mysql2/lib/connection.js:515:28)
    at PacketParser.onPacket (/var/opt/roundTable/node_modules/mysql2/lib/connection.js:94:16)
    at PacketParser.executeStart (/var/opt/roundTable/node_modules/mysql2/lib/packet_parser.js:77:14)
    at Socket.<anonymous> (/var/opt/roundTable/node_modules/mysql2/lib/connection.js:102:29)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:547:20)

Unhandled rejection SequelizeDatabaseError: Cannot add foreign key constraint
    at Query.formatError (/var/opt/roundTable/node_modules/sequelize/lib/dialects/mysql/query.js:222:16)
    at Query.connection.query [as onResult] (/var/opt/roundTable/node_modules/sequelize/lib/dialects/mysql/query.js:55:23)
    at Query.Command.execute (/var/opt/roundTable/node_modules/mysql2/lib/commands/command.js:30:12)
    at Connection.handlePacket (/var/opt/roundTable/node_modules/mysql2/lib/connection.js:515:28)
    at PacketParser.onPacket (/var/opt/roundTable/node_modules/mysql2/lib/connection.js:94:16)
    at PacketParser.executeStart (/var/opt/roundTable/node_modules/mysql2/lib/packet_parser.js:77:14)
    at Socket.<anonymous> (/var/opt/roundTable/node_modules/mysql2/lib/connection.js:102:29)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:547:20)

Executing (default): SHOW INDEX FROM `users`
Executing (default): SELECT `id`, `firstname`, `lastname`, `email_id`, `fb_id`, `password`, `comapany_id`, `user_type`, `anonymous`, `admin_anonymous`, `status`, `created_at`, `updated_at` FROM `users` AS `user`;
Executing (default): INSERT INTO `users` (`id`,`firstname`,`lastname`,`email_id`,`password`,`comapany_id`,`user_type`,`anonymous`,`admin_anonymous`,`status`,`created_at`,`updated_at`) VALUES (DEFAULT,'admin','admin','admin','changed',0,1,false,false,false,'2017-08-09 11:06:47','2017-08-09 11:06:47');

1 个答案:

答案 0 :(得分:1)

试试这个:

db.sequelize.query('SET FOREIGN_KEY_CHECKS = 0', { raw: true }).then ( function () {
    db.sequelize.sync ({ force: true }).then ( function () {
        // Do something...
    });
});