【发布时间】:2021-04-30 01:02:07
【问题描述】:
我有以下迁移代码:
exports.up = function(knex) {
return knex.schema.createTable('users', function(table){
table.increments();
table.string('email', 100).unique().notNullable();
table.string('password').notNullable();
})
.createTable('posts', function(table){
table.increments();
table.string('title').notNullable();
table.string('content').notNullable();
table.timestamp('created_at').defaultTo(knex.fn.now());
table.int('userId').notNullable().references('id').inTable('users');
})
.createTable('comments', function(table){
table.increments();
table.string('content').notNullable();
table.timestamp('created_at').defaultTo(knex.fn.now());
table.int('userId').notNullable().references('id').inTable('users');
table.int('postId').notNullable().references('id').inTable('posts');
})
};
exports.down = function(knex) {
return knex.schema.dropTable('users')
.dropTable('posts')
.dropTable('comments');
};
它产生以下错误:
migration failed with error: create table "posts" ("id" serial primary key, "title" varchar(255) not null, "content" varchar(255) not null, "created_at" timestamptz default CURRENT_TIMESTAMP, "userId" undefined not null) - type "undefined" does not exist
似乎很难连接用户和帖子表,但我不确定为什么。
【问题讨论】:
-
我照原样复制了它,它工作正常吗?不确定这是否是一回事,但您可能希望将
down函数更改为以相反的顺序放置。
标签: javascript knex.js