私は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)
は私です外部キーの制約が間違っている?
このエラーが発生する理由は何ですか?
返信いただきありがとうございます。