首页 > 解决方案 > node.js - UnhandledPromiseRejectedWarning:SQLITE_ERROR:表鞋没有名为“颜色”的列

问题描述

当我尝试使用正文中的外键将寄存器发布到“color_shoes”时,我创建了一个带有主键“id”的表“shoes”,以及一个以“Shoes”中的“id”作为外键的表“color_shoes”根据我的请求,我收到了这个错误:“UnhandledPromiseRejectedWarning: SQLITE_ERROR: table shoes has no column named 'color'”

这是在表“color_shoes”中创建寄存器的控制器功能

module.exports = {
    async create(request, response) {
        const { color, shoe_id } = request.body;

        const color_id = crypto.randomBytes(4).toString('HEX');

        await connection('shoes').insert({
            color_id,
            color,
            shoe_id,
        });

        return response.json({ color_id });
    }
}

这是到表“color_shoes”的迁移:

exports.up = function(knex) {
    return knex.schema.createTable('shoes_colors', function (table) {
        table.string('color_id').primary();
        table.string('color').notNullable();

        table.string('shoe_id').notNullable();

        table.foreign('shoe_id').references('id').inTable('shoes');
    })
};

exports.down = function(knex) {
    return knex.schema.dropTable('shoes_colors');
};

这是迁移到表“鞋子”:

exports.up = function(knex) {
  return knex.schema.createTable('shoes', function (table) {
      table.string('id').primary();
      table.string('model').notNullable();
      table.string('sponsor').notNullable();
  })
};

exports.down = function(knex) {
  return knex.schema.dropTable('shoes');
};

标签: javascriptnode.jssqlite

解决方案


推荐阅读