© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
3 replies
gfung

Column _ cannot be cast automatically to type integer

I have set a
postId
postId
column in my
comments
comments
table, in which I store the ID of the post the comment is attached too.
posts.id
posts.id
is of type
serial
serial
, and I set
postId
postId
to be an
integer
integer
, but I keep getting the error
column "postId" cannot be cast automatically to type integer
column "postId" cannot be cast automatically to type integer
with hint
You might need to specify "USING "postId"::integer".
You might need to specify "USING "postId"::integer".
How can I fix this?

Full code:
export const posts = pgTable('post', {
  id: serial('id').primaryKey(),
  createdAt: timestamp('createdAt').defaultNow(),
  title: text('title').notNull(),
  content: text('content'),
  likes: integer('likes').default(0),
  dislikes: integer('dislikes').default(0),
  userId: integer('userId')
    .notNull()
    .references(() => users.id),
  topic: topicsEnum('topics'),
  category: categoriesEnum('categories')
});

export const postRelations = relations(posts, ({ one, many }) => {
  user: one(users, {
    fields: [posts.userId],
    references: [users.id]
  });
  comments: many(comments);
});

export const comments = pgTable('comment', {
  id: serial('id').primaryKey(),
  comment: text('content'),
  likes: integer('likes').default(0),
  dislikes: integer('dislikes').default(0),
  postId: integer('postId')
    .notNull()
    .references(() => posts.id),
  createdAt: timestamp('createdAt').defaultNow()
});

export const commentRelations = relations(comments, ({ one }) => {
  post: one(posts, {
    fields: [comments.postId],
    references: [posts.id]
  });
});
export const posts = pgTable('post', {
  id: serial('id').primaryKey(),
  createdAt: timestamp('createdAt').defaultNow(),
  title: text('title').notNull(),
  content: text('content'),
  likes: integer('likes').default(0),
  dislikes: integer('dislikes').default(0),
  userId: integer('userId')
    .notNull()
    .references(() => users.id),
  topic: topicsEnum('topics'),
  category: categoriesEnum('categories')
});

export const postRelations = relations(posts, ({ one, many }) => {
  user: one(users, {
    fields: [posts.userId],
    references: [users.id]
  });
  comments: many(comments);
});

export const comments = pgTable('comment', {
  id: serial('id').primaryKey(),
  comment: text('content'),
  likes: integer('likes').default(0),
  dislikes: integer('dislikes').default(0),
  postId: integer('postId')
    .notNull()
    .references(() => posts.id),
  createdAt: timestamp('createdAt').defaultNow()
});

export const commentRelations = relations(comments, ({ one }) => {
  post: one(posts, {
    fields: [comments.postId],
    references: [posts.id]
  });
});
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

column "summary" cannot be cast automatically to type jsonb
Drizzle TeamDTDrizzle Team / help
3y ago
error: column "id" cannot be cast automatically to type uuid
Drizzle TeamDTDrizzle Team / help
3y ago
Cannot be cast automatically
Drizzle TeamDTDrizzle Team / help
11mo ago
applying migrations... PostgresError: column "x" cannot be cast automatically to type x
Drizzle TeamDTDrizzle Team / help
16mo ago