claudiu
claudiu
DTDrizzle Team
Created by claudiu on 2/28/2025 in #help
Migration failed, and can't add new migration
Hello, I've tried to add a change to a table
import { pgTable, serial, text } from 'drizzle-orm/pg-core';

export const categories = pgTable('categories', {
id: serial('id').primaryKey(),
name: text('name').notNull().unique(),
label: text('label').notNull()
});
import { pgTable, serial, text } from 'drizzle-orm/pg-core';

export const categories = pgTable('categories', {
id: serial('id').primaryKey(),
name: text('name').notNull().unique(),
label: text('label').notNull()
});
I run generate and migrate after and my migration failed because I table was already populated with data after the failure I changed to this
export const categories = pgTable('categories', {
id: serial('id').primaryKey(),
name: text('name').notNull().unique(),
label: text('label')
});
export const categories = pgTable('categories', {
id: serial('id').primaryKey(),
name: text('name').notNull().unique(),
label: text('label')
});
but it's still failing with this error
PostgresError: column "label" of relation "categories" contains null values
at ErrorResponse (file:///Users/claudiu/Development/fsega/node_modules/postgres/src/connection.js:788:26)
at handle (file:///Users/claudiu/Development/fsega/node_modules/postgres/src/connection.js:474:6)
at Socket.data (file:///Users/claudiu/Development/fsega/node_modules/postgres/src/connection.js:315:9)
at Socket.emit (node:events:519:28)
at addChunk (node:internal/streams/readable:559:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
at Readable.push (node:internal/streams/readable:390:5)
at TCP.onStreamRead (node:internal/stream_base_commons:191:23) {
severity_local: 'ERROR',
severity: 'ERROR',
code: '23502',
schema_name: 'public',
table_name: 'categories',
column_name: 'label',
file: 'tablecmds.c',
line: '6279',
routine: 'ATRewriteTable'
}
PostgresError: column "label" of relation "categories" contains null values
at ErrorResponse (file:///Users/claudiu/Development/fsega/node_modules/postgres/src/connection.js:788:26)
at handle (file:///Users/claudiu/Development/fsega/node_modules/postgres/src/connection.js:474:6)
at Socket.data (file:///Users/claudiu/Development/fsega/node_modules/postgres/src/connection.js:315:9)
at Socket.emit (node:events:519:28)
at addChunk (node:internal/streams/readable:559:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
at Readable.push (node:internal/streams/readable:390:5)
at TCP.onStreamRead (node:internal/stream_base_commons:191:23) {
severity_local: 'ERROR',
severity: 'ERROR',
code: '23502',
schema_name: 'public',
table_name: 'categories',
column_name: 'label',
file: 'tablecmds.c',
line: '6279',
routine: 'ATRewriteTable'
}
I checked the migration files
// BEFORE
ALTER TABLE "categories" ADD COLUMN "label" text NOT NULL;
// BEFORE
ALTER TABLE "categories" ADD COLUMN "label" text NOT NULL;
// AFTER
ALTER TABLE "categories" ALTER COLUMN "label" DROP NOT NULL;
// AFTER
ALTER TABLE "categories" ALTER COLUMN "label" DROP NOT NULL;
I see the second migration it's trying to alter the label column but that column doesn't exist becuase the previous migration failed. How I can handle this?
3 replies