【发布时间】:2017-10-16 12:29:24
【问题描述】:
我正在尝试使用 knex 中的外键创建以下表格:
评论
+----+---------+-------------------+------------+------------+------------+-----------+
| id | post_id | comment | is_deleted | createdAt | updatedAt | deletedAt |
+----+---------+-------------------+------------+------------+------------+-----------+
| 1 | 2 | This is a comment | false | 16.10.2017 | 16.10.2017 | |
+----+---------+-------------------+------------+------------+------------+-----------+
发帖
+----+-----------------+------------------+---------+------------+------------+-----------+
| id | titel | description | deleted | createdAt | updatedAt | deletedAt |
+----+-----------------+------------------+---------+------------+------------+-----------+
| 1 | This is a titel | Test Description | false | 16.10.2017 | 16.10.2017 | |
+----+-----------------+------------------+---------+------------+------------+-----------+
| 2 | Titel Test | Test Description | false | 16.10.2017 | 16.10.2017 | |
+----+-----------------+------------------+---------+------------+------------+-----------+
我创建了以下迁移:
comments.js
exports.up = function (knex, Promise) {
return knex.schema.createTable("comments", function (t) {
t.increments("id").unsigned().primary().references('id').inTable('posts')
t.text("comment").nullable()
t.boolean("is_deleted").nullable()
t.dateTime("createdAt").notNull()
t.dateTime("updatedAt").nullable()
t.dateTime("deletedAt").nullable()
})
}
posts.js
exports.up = function (knex, Promise) {
return knex.schema.createTable('posts', function (t) {
t.increments('id').unsigned().primary();
t.string('title').notNull();
t.text('description').nullable();
t.boolean('deleted').nullable();
t.dateTime('createdAt').notNull();
t.dateTime('updatedAt').nullable();
t.dateTime('deletedAt').nullable();
});
};
最后,我尝试用假数据为表格播种:
const faker = require("faker")
const knex = require("../db/knexfile.js")
const _ = require("lodash")
const postNumber = 50
const commentNumber = 150
function getRandomPostId() {
const numberOfPosts = knex("posts").count("title")
return _.random(0, numberOfPosts)
}
exports.seed = function(knex, Promise) {
return Promise.all([
knex("posts").del()
.then(function() {
const posts = []
for (let index = 0; index < postNumber; index++) {
posts.push({
titel: faker.lorem.sentence(),
description: faker.lorem.sentence(),
createdAt: faker.date.recent(),
updatedAt: faker.date.recent(),
deletedAt: faker.date.recent(),
deleted: faker.random.boolean(),
tags: faker.random.arrayElement(["tag1", "tag2", "tag3", "tag4", ]),
})
}
return knex("posts").insert(posts)
}),
knex("comments").del()
.then(function() {
const comments = []
for (let index = 0; index < commentNumber; index++) {
comments.push({
comment: faker.lorem.sentence(),
createdAt: faker.date.recent(),
deletedAt: faker.date.recent(),
updatedAt: faker.date.recent(),
is_deleted: faker.date.recent(),
})
}
return knex("comments").insert(comments)
})
])
}
但是,我收到以下错误:
Using environment: development
Error: ER_NO_REFERENCED_ROW_2: Cannot add or update a child row: a foreign key constraint fails (`c9`.`comments`, CONSTRAINT `comments_id_foreign` FOREIGN KEY (`id`) REFERENCES `posts` (`id`))
at Query.Sequence._packetToError (/home/ubuntu/workspace/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
at Query.ErrorPacket (/home/ubuntu/workspace/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)
at Protocol._parsePacket (/home/ubuntu/workspace/node_modules/mysql/lib/protocol/Protocol.js:279:23)
at Parser.write (/home/ubuntu/workspace/node_modules/mysql/lib/protocol/Parser.js:76:12)
at Protocol.write (/home/ubuntu/workspace/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/home/ubuntu/workspace/node_modules/mysql/lib/Connection.js:103:28)
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)
我的外键约束错了吗?
有什么建议为什么我会收到此错误?
感谢您的回复!
【问题讨论】:
标签: javascript node.js knex.js