Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drizzle-orm error: column "created_at" cannot be cast automatically to type timestamp without time zone

This is my schema.ts file

export const messages = pgTable("messages", {
  id: serial("id").primaryKey(),
  chatId: integer("chat_id")
    .references(() => chats.id)
    .notNull(),
  content: text("content").notNull(),
  createdAt: timestamp("created_at").notNull().defaultNow(),
  role: userSystemEnum("role").notNull(),
});

It is throwing error as:

error: column "created_at" cannot be cast automatically to type timestamp without time zone
    at Parser.parseErrorMessage (A:\sark and other\chatpdf-yt-main\node_modules\drizzle-kit\index.cjs:40722:98)
    at Parser.handlePacket (A:\sark and other\chatpdf-yt-main\node_modules\drizzle-kit\index.cjs:40563:25)     
    at Parser.parse (A:\sark and other\chatpdf-yt-main\node_modules\drizzle-kit\index.cjs:40487:34)
    at TLSSocket.<anonymous> (A:\sark and other\chatpdf-yt-main\node_modules\drizzle-kit\index.cjs:40763:44)   
    at TLSSocket.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Readable.push (node:internal/streams/readable:234:10)
    at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23) {
  length: 231,
  severity: 'ERROR',
  code: '42804',
  detail: undefined,
  hint: 'You might need to specify "USING created_at::timestamp without time zone".',
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'tablecmds.c',
  line: '12302',
  routine: 'ATPrepAlterColumnType'
}

For role also showing same error:

export const userSystemEnum = pgEnum("user_system_enum", ["system", "user"]);
hint: 'You might need to specify "USING role::user_system_enum".'

How can I fix this?

like image 380
Naveen Avatar asked Oct 21 '25 00:10

Naveen


2 Answers

I think you need to restructure the createdAt, i'm not really sure about what is wrong here. but maybe this would work:

  createdAt: timestamp("created_at").defaultNow().notNull(),
like image 172
Victor Timi Avatar answered Oct 24 '25 02:10

Victor Timi


As the hint suggests, append USING created_at::timestamp without time zone to the line of the generated SQL file that alters that column. So for your example you would change:

ALTER TABLE "messages"
ALTER COLUMN "created_at"
SET DATA TYPE timestamp without time zone

to

ALTER TABLE "messages"
ALTER COLUMN "created_at"
SET DATA TYPE timestamp without time zone USING ("created_at"::timestamp without time zone)
like image 30
Ben Stickley Avatar answered Oct 24 '25 01:10

Ben Stickley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!