Issue running migrations to DB
I am having an issue running migrations to Neon. I checked that everything is exporting const variables and tried add the cjs version of drizzle kit and reinstalling node modules.. but no luck fixing it
I also tried to run a migration on a new db and its still giving me the same error. not sure whats going on
v-app git:(drizzle-orm) ā pnpm generate
> v-app-v2@0.0.0 generate v-app
> drizzle-kit generate:pg
drizzle-kit: v0.19.2
drizzle-orm: v0.27.0
No config path provided, using default 'drizzle.config.ts'
Reading config file 'v-app/drizzle.config.ts'
36 tables
features 3 columns 0 indexes 0 fks
plans 4 columns 0 indexes 0 fks
plans_features 3 columns 0 indexes 1 fks
teams 2 columns 0 indexes 0 fks
users 9 columns 0 indexes 0 fks
...
views 11 columns 0 indexes 3 fks
6 enums
plans_enum [free, basic, pro, enterprise]
...
view_type [dynamic, static]
[ā] Your SQL migration file ā src/db/migrations/0010_material_lilandra.sql š
ā v-app git:(drizzle-orm) ā pnpm migrate
> v-app-v2@0.0.0 migrate
> tsx -r dotenv/config -r tsconfig-paths/register src/db/index.ts
{
severity_local: 'NOTICE',
severity: 'NOTICE',
code: '42P06',
message: 'schema "drizzle" already exists, skipping',
file: 'schemacmds.c',
line: '128',
routine: 'CreateSchemaCommand'
}
{
severity_local: 'NOTICE',
severity: 'NOTICE',
code: '42P07',
message: 'relation "__drizzle_migrations" already exists, skipping',
file: 'parse_utilcmd.c',
line: '209',
routine: 'transformCreateStmt'
}
node:internal/process/esm_loader:97
internalBinding('errors').triggerUncaughtException(
^
PostgresError: type "plans" already exists
v-app git:(drizzle-orm) ā pnpm generate
> v-app-v2@0.0.0 generate v-app
> drizzle-kit generate:pg
drizzle-kit: v0.19.2
drizzle-orm: v0.27.0
No config path provided, using default 'drizzle.config.ts'
Reading config file 'v-app/drizzle.config.ts'
36 tables
features 3 columns 0 indexes 0 fks
plans 4 columns 0 indexes 0 fks
plans_features 3 columns 0 indexes 1 fks
teams 2 columns 0 indexes 0 fks
users 9 columns 0 indexes 0 fks
...
views 11 columns 0 indexes 3 fks
6 enums
plans_enum [free, basic, pro, enterprise]
...
view_type [dynamic, static]
[ā] Your SQL migration file ā src/db/migrations/0010_material_lilandra.sql š
ā v-app git:(drizzle-orm) ā pnpm migrate
> v-app-v2@0.0.0 migrate
> tsx -r dotenv/config -r tsconfig-paths/register src/db/index.ts
{
severity_local: 'NOTICE',
severity: 'NOTICE',
code: '42P06',
message: 'schema "drizzle" already exists, skipping',
file: 'schemacmds.c',
line: '128',
routine: 'CreateSchemaCommand'
}
{
severity_local: 'NOTICE',
severity: 'NOTICE',
code: '42P07',
message: 'relation "__drizzle_migrations" already exists, skipping',
file: 'parse_utilcmd.c',
line: '209',
routine: 'transformCreateStmt'
}
node:internal/process/esm_loader:97
internalBinding('errors').triggerUncaughtException(
^
PostgresError: type "plans" already exists
1 Reply
this is my index.ts
config
So turns out I had a misspelling that I missed despite reading the table like 5 times š but once I fixed it I think I had to delete the migrations folder and regenerate everything to get the migration working
import { drizzle, PostgresJsDatabase } from "drizzle-orm/postgres-js"
import postgres from "postgres"
import { migrate } from "drizzle-orm/postgres-js/migrator"
import { config } from "dotenv"
config({ path: ".env" })
const connectionString = process.env.DATABASE_URL_TEST2 || ""
console.log(connectionString)
const client = postgres(connectionString, { ssl: "require" })
const db: PostgresJsDatabase = drizzle(client)
const migrationsClient = postgres(connectionString, {
max: 1,
ssl: "require",
})
const migrationsDb = drizzle(migrationsClient)
await migrate(migrationsDb, { migrationsFolder: "./src/db/migrations" })
console.log("Migration completed successfully")
export { db, migrationsDb, client, migrationsClient }
import { drizzle, PostgresJsDatabase } from "drizzle-orm/postgres-js"
import postgres from "postgres"
import { migrate } from "drizzle-orm/postgres-js/migrator"
import { config } from "dotenv"
config({ path: ".env" })
const connectionString = process.env.DATABASE_URL_TEST2 || ""
console.log(connectionString)
const client = postgres(connectionString, { ssl: "require" })
const db: PostgresJsDatabase = drizzle(client)
const migrationsClient = postgres(connectionString, {
max: 1,
ssl: "require",
})
const migrationsDb = drizzle(migrationsClient)
await migrate(migrationsDb, { migrationsFolder: "./src/db/migrations" })
console.log("Migration completed successfully")
export { db, migrationsDb, client, migrationsClient }
import type { Config } from "drizzle-kit"
export default {
schema: "./src/db/schema/*",
out: "./src/db/migrations",
// verbose: true,
} satisfies Config
import type { Config } from "drizzle-kit"
export default {
schema: "./src/db/schema/*",
out: "./src/db/migrations",
// verbose: true,
} satisfies Config