You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Drizzle ORM PostgreSQL中使用serial类型迁移报错:type 'serial' does not exist

解决Drizzle中serial类型报错"type 'serial' does not exist"的问题

可能的原因及解决步骤:

  • 数据库连接错误:你导入了drizzle-orm/pg-core(PostgreSQL专用核心库),但实际连接的可能是SQLite、MySQL等不支持serial类型的数据库。

    • 检查数据库连接配置,确保连接的是PostgreSQL实例。
  • PostgreSQL版本过低:serial是PostgreSQL的传统伪类型,若数据库版本低于9.1(老旧版本),可能不支持该类型。

    • 升级PostgreSQL到9.1及以上稳定版本。
  • Schema中的其他潜在问题(虽不直接触发serial报错,但会导致迁移失败):

    • posts表中code和language字段重复使用了text("content")作为列名,需修改列名避免冲突:
      export const posts = pgTable("post", {
        id: serial("id").notNull().primaryKey(),
        code: text("code"),
        language: text("language"),
        likes: integer("likes"),
        authorId: integer("author_id")
          .notNull()
          .references(() => users.id),
      });
      
    • likes表的postId为text类型,与关联的posts.id(serial本质是integer)类型不匹配,会导致外键创建失败,需调整类型:
      export const likes = pgTable("likes", {
        postId: integer("post_id").notNull().primaryKey(),
        authorId: integer("author_id"),
      });
      

内容的提问来源于stack exchange,提问作者Niklas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 18:46:13