diff --git a/db/knex_migrations/2026-05-28-0010-better-auth-foreign-key.js b/db/knex_migrations/2026-05-28-0010-better-auth-foreign-key.js index ef44233..b394d1b 100644 --- a/db/knex_migrations/2026-05-28-0010-better-auth-foreign-key.js +++ b/db/knex_migrations/2026-05-28-0010-better-auth-foreign-key.js @@ -31,20 +31,17 @@ const tables = [ exports.up = async function (knex) { for (const table of tables) { - try { - await knex.schema.alterTable(table.name, (t) => { - t.dropForeign("user_id"); - }); - } catch (error) { - // Not sure why it only happens on Ubuntu 22.04 + Node.js 26 - // Error: Can't DROP 'docker_host_user_id_foreign'; check that column/key exists - console.error(`Error dropping foreign key for table ${table.name}:`, error); - - const columns = await knex(table.name).columnInfo(); - console.error(`Schema for ${table.name}:`, JSON.stringify(columns, null, 2)); - - // Try to ignore it, because the foreign key not existing, we can just drop user_id - // throw error; + const isPostgres = knex.client.dialect === "postgresql" || knex.client.config.client === "pg"; + if (isPostgres) { + await knex.raw(`ALTER TABLE "${table.name}" DROP CONSTRAINT IF EXISTS "${table.name}_user_id_foreign"`); + } else { + try { + await knex.schema.alterTable(table.name, (t) => { + t.dropForeign("user_id"); + }); + } catch (error) { + console.error(`Error dropping foreign key for table ${table.name}:`, error.message); + } } await knex.schema.alterTable(table.name, (t) => {