knex 不 运行 使用外键迁移

knex doesn't run migrations with foreign keys

努力与 Knex.js 成功迁移。我有 3 个表(用户、员工、客户)。员工和客户有来自用户的 FK。但是,如果我使用 .foreign 属性,我将无法进行迁移,但是任何其他 属性(可以说我已经尝试使用 bigInt)运行 都很好。我在一篇文章中看到,首先创建 PK 很重要,因此根据我的理解,到目前为止我的用户应该 运行 首先。 我得到的错误:

migration failed with error: alter table "clients" add constraint "clients_user_id_foreign" foreign key ("user_id") references "users" ("user_id") on delete CASCADE - column "user_id" referenced in foreign key constraint does not exist

20220216185323_users:

const {hashPassword} = require("../../utils/bcryptUtils");
exports.up = function (knex) {
    return knex.schema.createTable("users", table => {
        table.increments("user_id").primary();
        table.string("username").unique().notNullable();
        table.string("password").notNullable();
        table.string("email").unique().notNullable();
        table.string("role").defaultTo("Client");
        table.string("type").defaultTo("Client");
        table.timestamp("created_at").defaultTo(knex.fn.now());
    }).then(async function () {
        const {hashPassword} = require("../../utils/bcryptUtils");

        return knex("users").insert([
            {
                "username": process.env.USERNAME,
                "password": await hashPassword(process.env.PASSWORD),
                "email": process.env.EMAIL,
                "role": process.env.ROLE,
                "type": process.env.TYPE
            }
        ]);
    });
};

exports.down = function (knex) {
    return knex.schema.dropTable("users");
};

20220217200112_clients:

exports.up = function (knex) {
    return knex.schema.createTable("clients", table => {
        table.increments("client_id").primary();
        table.foreign("user_id").references("user_id").inTable("users").onDelete("CASCADE");
        table.string("client_name").notNullable();
        table.string("client_email").unique().notNullable();
        table.string("client_vat").unique().notNullable();
        table.string("client_auth_key").notNullable();
        table.string("client_plan").defaultTo("Bronze");
        table.timestamp("created_at").defaultTo(knex.fn.now());
    });
};

exports.down = function (knex) {
    return knex.schema.dropTable("clients")
};

20220217200128_employees:

exports.up = function (knex) {
    return knex.schema.createTable("employees", table => {
        table.increments("employee_id").primary();
        table.foreign("user_id").references("user_id").inTable("users").onDelete("CASCADE");
        table.string("employee_firstname").defaultTo(null);
        table.string("employee_lastname").defaultTo(null);
        table.string("employee_email").defaultTo(null);
        table.timestamp("created_at").defaultTo(knex.fn.now());
    });
};

exports.down = function (knex) {
    return knex.schema.dropTable("employees");
};

您似乎缺少 employeesclient 表中 user_id 外键列的类型。如果您添加类型,它应该可以工作。

exports.up = function (knex) {
    return knex.schema.createTable("clients", table => {
        table.increments("client_id").primary();
        table.integer("user_id").unsigned();
        table.foreign("user_id").references("user_id").inTable("users").onDelete("CASCADE");
        table.string("client_name").notNullable();
        table.string("client_email").unique().notNullable();
        table.string("client_vat").unique().notNullable();
        table.string("client_auth_key").notNullable();
        table.string("client_plan").defaultTo("Bronze");
        table.timestamp("created_at").defaultTo(knex.fn.now());
    });
};
exports.up = function (knex) {
    return knex.schema.createTable("employees", table => {
        table.increments("employee_id").primary();
        table.integer("user_id").unsigned();
        table.foreign("user_id").references("user_id").inTable("users").onDelete("CASCADE");
        table.string("employee_firstname").defaultTo(null);
        table.string("employee_lastname").defaultTo(null);
        table.string("employee_email").defaultTo(null);
        table.timestamp("created_at").defaultTo(knex.fn.now());
    });
};