无法使用knex创建表并向数据库发送数据,我在表之间的关联方面有问题



我试图创建三个表与knex,但我不知道如果我试图创建它们的方式已经改变了,因为我最后一次创建它3年前这种方式。我的用户迁移如下:

exports.up = function(knex) {
return knex.schema.createTable('users', table => {
table.increments('id').primary()
table.string('name').notNull()
table.integer('age').notNull()
table.string('city').notNull()
table.string('email').notNull().unique()
table.string('primary').notNull()
table.timestamp('deletedAt')
table.timestamp('updatedAt')
});
};
exports.down = function(knex) {
return knex.schema.dropTable('users');
};

而后迁移是这样的:

exports.up = function(knex) {
return knex.schema.createTable('posts', table => {
table.increments('id').primary()
table.string('description').notNull()
table.string('image_url', 1000).notNull()
table.string('latitude').notNull()
table.string('longitude').notNull()
table.integer('userId').references('id')
.inTable('users').notNull()
table.timestamp('deletedAt')
table.timestamp('updatedAt')
});
};
exports.down = function(knex) {
return knex.schema.dropTable('posts');
};

最后一张表是评估:

exports.up = function(knex) {
return knex.schema.createTable('evaluations', table => {
table.inplements('id').primary()
table.int('review').notNull()
table.decimal('rate', (1,2)).notNull()
table.integer('postId').references('id')
.inTable('posts').notNull()
table.integer('userId').references('id')
.inTable('users').notNull()
table.timestamp('deletedAt')
table.timestamp('updatedAt')
});
};
exports.down = function(knex) {
return knex.schema.dropTable('evaluations');
};

当我运行命令knex migrate:latest时,我在数据库中创建了users表和posts表,但是没有创建评估表,并且我在提示符处得到这个错误:

migration file "20210902232536_create_posts_table.js" failed
migration failed with error: alter table `posts` add constraint `posts_userid_foreign` foreign key (`userId`) references `users` (`id`) - Referencing column 'userId' and referenced column 'id' in foreign key constraint 'posts_userid_foreign' are incompatible.
alter table `posts` add constraint `posts_userid_foreign` foreign key (`userId`) references `users` (`id`) - Referencing column 'userId' and referenced column 'id' in foreign key constraint 'posts_userid_foreign' are incompatible.
Error: alter table `posts` add constraint `posts_userid_foreign` foreign key (`userId`) references `users` (`id`) - Referencing column 'userId' and referenced column 'id' in foreign key constraint 'posts_userid_foreign' are incompatible.
at Packet.asError (F:nodejstestnode_modulesmysql2libpacketspacket.js:722:17)
at Query.execute (F:nodejstestnode_modulesmysql2libcommandscommand.js:28:26)
at Connection.handlePacket (F:nodejstestnode_modulesmysql2libconnection.js:456:32)
at PacketParser.onPacket (F:nodejstestnode_modulesmysql2libconnection.js:85:12)
at PacketParser.executeStart (F:nodejstestnode_modulesmysql2libpacket_parser.js:75:16)
at Socket.<anonymous> (F:nodejstestnode_modulesmysql2libconnection.js:92:25)
at Socket.emit (events.js:314:20)
at addChunk (_stream_readable.js:297:12)
at readableAddChunk (_stream_readable.js:272:9)
at Socket.Readable.push (_stream_readable.js:213:10

我不知道这是怎么发生的与关联,因为我以前用同样的方式创建它,但现在看来,我没有id到用户表或到帖子表我有这里是我的package。json:

{
"name": "test",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"dev": "nodemon"
},
"dependencies": {
"bcrypt": "^5.0.1",
"consign": "^0.1.6",
"cors": "^2.8.5",
"express": "^4.17.1",
"jwt-simple": "^0.5.6",
"knex": "^0.95.10",
"moment": "^2.29.1",
"mysql2": "^2.3.0",
"node-schedule": "^2.0.0",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0"
},
"devDependencies": {
"nodemon": "^2.0.12"
}
}

我很感激任何人给我的帮助

一旦我在这里没有未发誓,我必须找到哪里是knex lib的文档,所以我看到很多事情与3年前不同,经过长时间的研究,我终于能够自己解决这个问题:

My users migration is same as before.

但是现在必须这样创建post迁移:

exports.up = function(knex) {
return knex.schema.createTable('posts', table => {
table.increments('id').primary()
table.string('description').notNull()
table.string('image_url', 1000).notNull()
table.string('latitude').notNull()
table.string('longitude').notNull()
table.integer('userId').unsigned().notNullable()
.references('id').inTable('users')
table.timestamp('updatedAt')
});
};
exports.down = function(knex) {
return knex.schema.dropTable('posts');
};

评估迁移是这样设置的:

exports.up = function(knex) {
return knex.schema.createTable('evaluations', table => {
table.increments('id').primary()
table.integer('review').notNull()
table.decimal('rate', (1,2)).notNull()
table.integer('postId').unsigned().notNullable()
.references('id').inTable('posts')
table.integer('userId').unsigned().notNullable()
.references('id').inTable('users')
table.timestamp('deletedAt')
});
};
exports.down = function(knex) {
return knex.schema.dropTable('evaluations');
};

如果其他人在使用knex在迁移中创建外键时遇到了这个问题,这就是我找到的方法。