在 knex migration:up 上,table.foreign 没有显示在终端的实际 table 上
On knex migration:up, table.foreign is not showing up on the actual table on the terminal
我已经设置了 knex 迁移来创建两个 tables
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;
时,userid
在usersloc中找不到。
我的意图是将它引用到“用户”table 的 id
。
知道发生了什么吗?
找到解决方案 - 看起来 UUID 需要在两个表上都有一个 .unique() 引用。我的猜测是,即使 UUID 可能需要数十亿年才能有 1% 的碰撞机会,但它仍然不是唯一的 ID,因此需要 .unique() 引用。
我已经设置了 knex 迁移来创建两个 tables
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;
时,userid
在usersloc中找不到。
我的意图是将它引用到“用户”table 的 id
。
知道发生了什么吗?
找到解决方案 - 看起来 UUID 需要在两个表上都有一个 .unique() 引用。我的猜测是,即使 UUID 可能需要数十亿年才能有 1% 的碰撞机会,但它仍然不是唯一的 ID,因此需要 .unique() 引用。