首页 > 解决方案 > 在 knex 迁移:上,table.foreign 未显示在终端的实际表上

问题描述

我已经设置 knex 迁移来创建两个表

export async function up(knex: Knex): Promise<void> {

    const table1 = await knex.schema.hasTable('users');
    if (!table1) {
        await knex.schema.createTable('users', (table) => {
            table.increments("id").primary();
            table.string("username");
            table.string("email");
            table.string("password");
            table.string("role");
            table.timestamp("created_at");
            table.timestamp("updated_at");
            table.timestamp("logged_at");
        });
    }

    const table2 = await knex.schema.hasTable('usersloc');
    if (!table2) {
        await knex.schema.createTable('usersloc', (table) => {
            table.increments("locid");
            table.string("lat");
            table.string("lng")
            table.foreign("userid");
        });
    }
}

但是,当我执行select * from usersloc;, useridin usersloc 时,无处可寻。

我的意图是将其引用到id“用户”表中。

知道发生了什么吗?

标签: databasetypescriptpostgresqldatabase-designknex.js

解决方案


找到解决方案 - 看起来 UUID 需要在两个表上都有一个 .unique() 引用。我的猜测是,尽管 UUID 可能需要数十亿年才能有 1% 的碰撞机会,但它仍然不是唯一 ID,因此需要 .unique() 引用。


推荐阅读