I have a user table: import { Knex } from 'knex' const tableName = 'users' export async…
- User
I have a user table:
import { Knex } from 'knex'
const tableName = 'users'
export async function up(knex: Knex): Promise { return Promise.all([ knex.raw('create extension if not exists "uuid-ossp"'), knex.schema.createTable(tableName, (table) => { table .uuid('id') .primary() .defaultTo(knex.raw('uuid_generate_v4()')) .notNullable() table.string('email').unique().notNullable() table.string('role').notNullable() table.string('phone').notNullable() table.string('full_name').notNullable() table.string('password_hash').notNullable() table.timestamp('created_at').notNullable().defaultTo(knex.fn.now()) table.timestamp('updated_at').notNullable().defaultTo(knex.fn.now()) table.timestamp('deleted_at').nullable() }), ]) }
export async function down(knex: Knex): Promise { return knex.schema.dropTable(tableName) }
and i want to have a verification_resources table that has a one to one relationship with a user in the users table by UUID. The columns should be id, channel, SID. Where id is a UUID, channel is a string, and SID is a string. Create for me the migration! id should be primary
- Assistant
Sure! Here is the migration code for creating a verification_resources table that has a one-to-one relationship with the users table:
jsimport { Knex } from 'knex' const tableName = 'verification_resources' export async function up(knex: Knex): Promise<any | void> { return knex.schema.createTable(tableName, (table) => { table .uuid('id') .primary() .defaultTo(knex.raw('uuid_generate_v4()')) .notNullable() table.string('channel').notNullable() table.string('SID').notNullable() table .uuid('user_id') .notNullable() .references('id') .inTable('users') .onDelete('CASCADE') .onUpdate('CASCADE') table.timestamp('created_at').notNullable().defaultTo(knex.fn.now()) table.timestamp('updated_at').notNullable().defaultTo(knex.fn.now()) table.timestamp('deleted_at').nullable() }) } export async function down(knex: Knex): Promise<void> { return knex.schema.dropTable(tableName) }