typescript - خطای drizzle-orm: ستون "created_at" نمیتواند بهطور خودکار به نوع timestamp بدون منطقه زمانی تبدیل شود
این فایل schema.ts من است
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(),
});
این خطا را نمایش میدهد به صورت:
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'
}
برای نقش نیز همان خطا نمایش داده میشود:
export const userSystemEnum = pgEnum("user_system_enum", ["system", "user"]);
hint: 'You might need to specify "USING role::user_system_enum".'
چطور میتوانم این را درست کنم؟
پاسخ شماره ۱
فکر میکنم نیاز باشد createdAt را بازسازی کنی، من واقعاً مطمئن نیستم مشکل اینجا چیست. اما شاید این کار جواب بده:
createdAt: timestamp("created_at").defaultNow().notNull(),
پاسخ شماره ۲
همانطور که راهنما نشان میدهد، USING created_at::timestamp without time zone را به خط فایل SQL تولید شده که آن ستون را تغییر میدهد اضافه کنید. بنابراین برای مثال شما تغییر میکنید:
ALTER TABLE "messages"
ALTER COLUMN "created_at"
SET DATA TYPE timestamp without time zone
به
ALTER TABLE "messages"
ALTER COLUMN "created_at"
SET DATA TYPE timestamp without time zone USING ("created_at"::timestamp without time zone)
