nodejs-koa-blog icon indicating copy to clipboard operation
nodejs-koa-blog copied to clipboard

npm start 启动报错

Open leo0828 opened this issue 6 years ago • 3 comments

npm start 启动报错,报错信息是不能加入有约束的外键

koa deprecated Support for generators will be removed in v3. See the documentation for examples of how to convert old middleware https://github.com/koajs/koa/blob/master/docs/migration.md app.js:18:5
Executing (default): CREATE TABLE IF NOT EXISTS `user` (`id` INTEGER(11) NOT NULL auto_increment , `username` VARCHAR(50) NOT NULL, `password` VARCHAR(255) NOT NULL, `createdAt` DATETIME, `updatedAt` DATETIME, PRIMARY KEY (`id`)) ENGINE=InnoDB;
Executing (default): CREATE TABLE IF NOT EXISTS `article` (`id` INTEGER auto_increment , `title` VARCHAR(255) NOT NULL, `author` VARCHAR(255) NOT NULL, `recommend` TINYINT(1) NOT NULL DEFAULT false, `introduce` VARCHAR(255) NOT NULL, `category` VARCHAR(255) NOT NULL, `banner` VARCHAR(255) NOT NULL, `content` TEXT NOT NULL, `browser` INTEGER DEFAULT 0, `createdAt` DATETIME, `updatedAt` DATETIME, `categoryId` INTEGER, PRIMARY KEY (`id`), FOREIGN KEY (`categoryId`) REFERENCES `category` (`id`) ON DELETE SET NULL ON UPDATE CASCADE) ENGINE=InnoDB;
Executing (default): CREATE TABLE IF NOT EXISTS `category` (`id` INTEGER auto_increment , `name` VARCHAR(50) NOT NULL, `createdAt` DATETIME, `updatedAt` DATETIME, PRIMARY KEY (`id`)) ENGINE=InnoDB;
Unhandled rejection SequelizeDatabaseError: Cannot add foreign key constraint
    at Query.formatError (/Users/Leo/my/projects/test/nodejs-koa2-mysql-sequelize-jwt/node_modules/sequelize/lib/dialects/mysql/query.js:247:16)
    at Query.handler [as onResult] (/Users/Leo/my/projects/test/nodejs-koa2-mysql-sequelize-jwt/node_modules/sequelize/lib/dialects/mysql/query.js:68:23)
    at Query.Command.execute (/Users/Leo/my/projects/test/nodejs-koa2-mysql-sequelize-jwt/node_modules/mysql2/lib/commands/command.js:30:12)
    at Connection.handlePacket (/Users/Leo/my/projects/test/nodejs-koa2-mysql-sequelize-jwt/node_modules/mysql2/lib/connection.js:502:28)
    at PacketParser.onPacket (/Users/Leo/my/projects/test/nodejs-koa2-mysql-sequelize-jwt/node_modules/mysql2/lib/connection.js:81:16)
    at PacketParser.executeStart (/Users/Leo/my/projects/test/nodejs-koa2-mysql-sequelize-jwt/node_modules/mysql2/lib/packet_parser.js:77:14)
    at Socket.<anonymous> (/Users/Leo/my/projects/test/nodejs-koa2-mysql-sequelize-jwt/node_modules/mysql2/lib/connection.js:89:29)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)
    at TCP.onread (net.js:597:20)

Executing (default): SHOW INDEX FROM `user`
Executing (default): SHOW INDEX FROM `category`

leo0828 avatar Dec 25 '18 10:12 leo0828

npm start 启动报错,报错信息是不能加入有约束的外键

koa deprecated Support for generators will be removed in v3. See the documentation for examples of how to convert old middleware https://github.com/koajs/koa/blob/master/docs/migration.md app.js:18:5
Executing (default): CREATE TABLE IF NOT EXISTS `user` (`id` INTEGER(11) NOT NULL auto_increment , `username` VARCHAR(50) NOT NULL, `password` VARCHAR(255) NOT NULL, `createdAt` DATETIME, `updatedAt` DATETIME, PRIMARY KEY (`id`)) ENGINE=InnoDB;
Executing (default): CREATE TABLE IF NOT EXISTS `article` (`id` INTEGER auto_increment , `title` VARCHAR(255) NOT NULL, `author` VARCHAR(255) NOT NULL, `recommend` TINYINT(1) NOT NULL DEFAULT false, `introduce` VARCHAR(255) NOT NULL, `category` VARCHAR(255) NOT NULL, `banner` VARCHAR(255) NOT NULL, `content` TEXT NOT NULL, `browser` INTEGER DEFAULT 0, `createdAt` DATETIME, `updatedAt` DATETIME, `categoryId` INTEGER, PRIMARY KEY (`id`), FOREIGN KEY (`categoryId`) REFERENCES `category` (`id`) ON DELETE SET NULL ON UPDATE CASCADE) ENGINE=InnoDB;
Executing (default): CREATE TABLE IF NOT EXISTS `category` (`id` INTEGER auto_increment , `name` VARCHAR(50) NOT NULL, `createdAt` DATETIME, `updatedAt` DATETIME, PRIMARY KEY (`id`)) ENGINE=InnoDB;
Unhandled rejection SequelizeDatabaseError: Cannot add foreign key constraint
    at Query.formatError (/Users/Leo/my/projects/test/nodejs-koa2-mysql-sequelize-jwt/node_modules/sequelize/lib/dialects/mysql/query.js:247:16)
    at Query.handler [as onResult] (/Users/Leo/my/projects/test/nodejs-koa2-mysql-sequelize-jwt/node_modules/sequelize/lib/dialects/mysql/query.js:68:23)
    at Query.Command.execute (/Users/Leo/my/projects/test/nodejs-koa2-mysql-sequelize-jwt/node_modules/mysql2/lib/commands/command.js:30:12)
    at Connection.handlePacket (/Users/Leo/my/projects/test/nodejs-koa2-mysql-sequelize-jwt/node_modules/mysql2/lib/connection.js:502:28)
    at PacketParser.onPacket (/Users/Leo/my/projects/test/nodejs-koa2-mysql-sequelize-jwt/node_modules/mysql2/lib/connection.js:81:16)
    at PacketParser.executeStart (/Users/Leo/my/projects/test/nodejs-koa2-mysql-sequelize-jwt/node_modules/mysql2/lib/packet_parser.js:77:14)
    at Socket.<anonymous> (/Users/Leo/my/projects/test/nodejs-koa2-mysql-sequelize-jwt/node_modules/mysql2/lib/connection.js:89:29)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)
    at TCP.onread (net.js:597:20)

Executing (default): SHOW INDEX FROM `user`
Executing (default): SHOW INDEX FROM `category`

嗯,我看了你的报错信息得出原因是:你的数据库用户分类表冲突了,非常抱歉,我周六时候更新了一个多表关联,导致前面的数据库表需要重新创建了

解决方法:你删除掉数据库表 user,category,和article,然后重新启动,:)

lfb avatar Dec 26 '18 00:12 lfb

OK,还有个问题我想问下,schema中定义模型的createdAt和updatedAt,我想知道他是预留字段吗?怎么做到创建的时候自动添加createdAt,更新的时候自动更新updatedAt?我没有看到相关的修改或者赋值的代码,我只在官网看到说可以通过get()自定义getter,使用this.getDataValue(String)操作基础值。我是个node新手,数据库这块不太了解,见谅! 另外,有没有其他的联系方式以便我们可以私下沟通的,谢谢。

leo0828 avatar Dec 28 '18 08:12 leo0828

OK,还有个问题我想问下,schema中定义模型的createdAt和updatedAt,我想知道他是预留字段吗?怎么做到创建的时候自动添加createdAt,更新的时候自动更新updatedAt?我没有看到相关的修改或者赋值的代码,我只在官网看到说可以通过get()自定义getter,使用this.getDataValue(String)操作基础值。我是个node新手,数据库这块不太了解,见谅! 另外,有没有其他的联系方式以便我们可以私下沟通的,谢谢。

现在才发现有回复,回复晚了,非常抱歉。首先默认情况下,Sequelize会将createdAt和updatedAt的属性添加到模型中,以便您可以知道数据库条目何时进入数据库以及何时被更新。然后你在操作一些数据或者更新数据时候,可以操作这两个字段的,共勉:)

lfb avatar Jan 02 '19 00:01 lfb