Oops! Looks like drizzle-orm ran into a hiccup trying to convert the "created_at" column to a timestamp without a time zone

This is the content of 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(),
});

An error is being thrown stating:

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'
}

The same error is also appearing for the 'role' field:

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

What steps should I take to resolve this issue?

Answer №1

It appears that a restructuring of the createdAt field is necessary as there might be an issue with its current setup. One potential solution could involve implementing the following code:

createdAt: timestamp("created_at").defaultNow().notNull(),

Answer №2

For the solution, follow the hint by adding

USING created_at::timestamp without time zone
to the line in the SQL file that modifies the column. So in your case, you should update:

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)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having issues with @ts-ignore in Typescript on a let variable that is not reassigned?

JOURNEY TO THE PROBLEM My current task involves destructuring a response obtained from an Apollo useLazyQuery, with the intention to modify one variable. In a non-Typescript environment, achieving this would be straightforward with just two lines of code: ...

Is it possible to generate code snippets for TypeScript in Visual Studio 2015?

Is it possible to create a code snippet for Angular 1.x directives written in TypeScript, even though there may be some product level configuration required? I have come across similar questions discussing TypeScript snippets on Visual Studio, but the res ...

Building basic objects in TypeScript

My current project involves utilizing an interface for vehicles. export interface Vehicle { IdNumber: number; isNew: boolean; contact: { firstName: string; lastName: string; cellPhoneNumber: number; ...

Typescript is being lenient with incorrect use of generics, contrary to my expectations of error being thrown

Encountered a puzzling Typescript behavior that has left me confused. Take a look at the following code snippet: interface ComponentProps<T> { oldObject: T } function Component<T>({ oldObject }: ComponentProps<T>) { const newObject = ...

Revised: "Mastering the Art of using useLoaderData Properly with Remix V2

It seems that the correct way to type useLoaderData has changed since V2. export const loader = async () => { return json({ messages: [...] }) } // In component... const { messages } = useLoaderData<typeof loader> Prior examples show it type ...

Having difficulty retrieving an angular file from a location outside of the asset folder

I'm encountering issues with a small project that is meant to read a log and present it in table format. Here is the outline of the project structure: project structure Within the LOG directory, I should be able to access motore.log from my DataServi ...

Typescript is encountering errors indicating that it is unable to locate modules for imported assets, such as images

Having trouble with TS not recognizing image imports. Although the site runs fine, TypeScript seems to have an issue identifying them: import React, { Component } from 'react'; import SlackIcon from './assets/social/slack-icon-thumb.png&apos ...

Turn off pm2 logging

I have my node app running via pm2 with the following command: pm2 start npm -- start --node-args="--max-old-space-size=1024" My package.json file contains: { "name": "my-app", "version": "1.0.0", &q ...

How can one use an Angular Route to navigate to a distinct URL? Essentially, how does one disable matching in the process?

I'm working on a front-end Angular application and I need to add a menu item that links to an external website. For example, let's say my current website has this URL: And I want the menu item in my app to lead to a completely different website ...

Why doesn't package.json typically utilize the preset values stored in the .npmrc file?

Windows 10 x64 Based on the information found here, I created a file called C:\Users\bushm\.npmrc with the following content: author = "Andrey Bushman" However, when I run the command npm init -y in a new directory, I noticed that the pac ...

Steps to prevent closing the alert box when clicking outside of it in Ionic

I am currently developing an Ionic 2 app and I have implemented the following component: http://ionicframework.com/docs/components/#alert import { AlertController } from 'ionic-angular'; export class MyPage { constructor(public alertCtrl: Al ...

Try logging in again if an error occurs

I've encountered some failing tests that we suspect are caused by network drops. To address this problem, I have modified my login method to retry after an error is detected. I would also like to have the number of retry attempts displayed in the cons ...

Is there a way to increase the level of detail in the error trace provided by tsc? This error trace is

While attempting to compile a React project using the default tsconfig.json provided by create-react-app, I encountered a baffling error that has me stumped. $ tsc error TS2488: Type 'never' must have a '[Symbol.iterator]()' method that ...

The Redux state fails to update on the initial attempt

I have encountered an issue with my state setup and reducer logic. Here is how my state is initialized: const initialState: PhotoState = { photos: [], }; The reducer function is defined as follows: const initialState: PhotoState = { photos: [], }; ex ...

Issue encountered with the props type upon import, Ts(2322)

Seeking assistance with a TypeScript, StyledComponent, and React project using Create React App. Encountering an error during build time that cannot be ignored. // browser and terminal error TypeScript error in ./src/index.tsx(4,1): Type '{ alt: ...

The function getStaticPaths() will generate a 404 error, indicating that the page

I have encountered a persistent issue with the getStaticPaths() function throwing a 404 error. After investigating, I suspect that the problem may lie in the implementation of the getAllPostIds() function, which is supposed to generate an array of object ...

Refine specific unions by considering past values that have been used

Here's the scenario at hand: type Option = 'x' | 'y' | 'z' | 'w' type Inquiry = { query: string; choices: Option[]; defaultChoice: Option // here's where it gets tricky } I am looking to set the def ...

Mismatch between TypeScript library and Angular web application: certain properties are absent

I am currently facing a dilemma with my two angular projects. The first one is a library while the second one is a regular Angular webapp. Within my library project, I have the following code snippet: class User { firstName: string; lastName: stri ...

What is the best way to treat each TS file as its own independent module?

Just starting out in the world of TS and feeling like a newbie. I've noticed that in Dart, each file in a directory can run independently and you have to explicitly import objects from other files if needed. For example: file1.dart int myFunc() => ...

Beautiful ExpressionChangedAfterItHasBeenCheckedError

I need your help Input field where I can enter a Student email or IAM, which will be added to a string array List displaying all the students I have added, using a for loop as shown below Delete button to remove a student from the array The list has a sp ...