drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
24.49k stars 643 forks source link

[BUG]: drizzle-kit push TypeError Cannot use 'in' operator to search for 'default' in undefined #2385

Closed alexisveryreal closed 5 months ago

alexisveryreal commented 5 months ago

What version of drizzle-orm are you using?

0.30.10

What version of drizzle-kit are you using?

0.21.4

Describe the Bug

run drizzle-kit push get error

 bun run db:push
$ bunx drizzle-kit push
drizzle-kit: v0.21.4
drizzle-orm: v0.30.10

No config path provided, using default path
Reading config file 'G:\dev\plan-o-clock\drizzle.config.ts'
Using 'pg' driver for database querying
[✓] Pulling schema from database...TypeError: Cannot use 'in' operator to search for 'default' in undefined
    at G:\dev\plan-o-clock\node_modules\drizzle-kit\bin.cjs:18362:23
    at Array.map (<anonymous>)
    at alternationsInColumn (G:\dev\plan-o-clock\node_modules\drizzle-kit\bin.cjs:18361:10)
    at G:\dev\plan-o-clock\node_modules\drizzle-kit\bin.cjs:18321:49
    at Array.map (<anonymous>)
    at findAlternationsInTable (G:\dev\plan-o-clock\node_modules\drizzle-kit\bin.cjs:18321:37)
    at G:\dev\plan-o-clock\node_modules\drizzle-kit\bin.cjs:18203:14
    at Array.map (<anonymous>)
    at applyJsonDiff (G:\dev\plan-o-clock\node_modules\drizzle-kit\bin.cjs:18201:69)
    at applyPgSnapshotsDiff (G:\dev\plan-o-clock\node_modules\drizzle-kit\bin.cjs:19822:26)

I had to drop all my tables and only then it worked

bun db:push
$ bunx drizzle-kit push
drizzle-kit: v0.21.4
drizzle-orm: v0.30.10

No config path provided, using default path
Reading config file 'G:\dev\plan-o-clock\drizzle.config.ts'
Using 'pg' driver for database querying
[✓] Pulling schema from database...[✓] Changes applied

however running it again with NO changes to schema or anything it shows the error again

bun db:push
$ bunx drizzle-kit push
drizzle-kit: v0.21.4
drizzle-orm: v0.30.10

No config path provided, using default path
Reading config file 'G:\dev\plan-o-clock\drizzle.config.ts'
Using 'pg' driver for database querying
[✓] Pulling schema from database...[✓] Changes applied

G://plan-o-clock on  7-feat-board-components ≡  ~4
16:49:14 ❯ bun db:push
$ bunx drizzle-kit push
drizzle-kit: v0.21.4
drizzle-orm: v0.30.10

No config path provided, using default path
Reading config file 'G:\dev\plan-o-clock\drizzle.config.ts'
Using 'pg' driver for database querying
[✓] Pulling schema from database...TypeError: Cannot use 'in' operator to search for 'default' in undefined
    at G:\dev\plan-o-clock\node_modules\drizzle-kit\bin.cjs:18362:23
    at Array.map (<anonymous>)
    at alternationsInColumn (G:\dev\plan-o-clock\node_modules\drizzle-kit\bin.cjs:18361:10)
    at G:\dev\plan-o-clock\node_modules\drizzle-kit\bin.cjs:18321:49
    at Array.map (<anonymous>)
    at findAlternationsInTable (G:\dev\plan-o-clock\node_modules\drizzle-kit\bin.cjs:18321:37)
    at G:\dev\plan-o-clock\node_modules\drizzle-kit\bin.cjs:18203:14
    at Array.map (<anonymous>)
    at applyJsonDiff (G:\dev\plan-o-clock\node_modules\drizzle-kit\bin.cjs:18201:69)
    at applyPgSnapshotsDiff (G:\dev\plan-o-clock\node_modules\drizzle-kit\bin.cjs:19822:26)

The error is also present when using npm

npm run db:push

> plan-o-clock@0.1.0 db:push
> npx drizzle-kit push

drizzle-kit: v0.21.4
drizzle-orm: v0.30.10

No config path provided, using default path
Reading config file 'G:\dev\plan-o-clock\drizzle.config.ts'
Using 'pg' driver for database querying
[✓] Pulling schema from database...TypeError: Cannot use 'in' operator to search for 'default' in undefined
    at G:\dev\plan-o-clock\node_modules\drizzle-kit\bin.cjs:18362:23
    at Array.map (<anonymous>)
    at alternationsInColumn (G:\dev\plan-o-clock\node_modules\drizzle-kit\bin.cjs:18361:10)
    at G:\dev\plan-o-clock\node_modules\drizzle-kit\bin.cjs:18321:49
    at Array.map (<anonymous>)
    at findAlternationsInTable (G:\dev\plan-o-clock\node_modules\drizzle-kit\bin.cjs:18321:37)
    at G:\dev\plan-o-clock\node_modules\drizzle-kit\bin.cjs:18203:14
    at Array.map (<anonymous>)
    at applyJsonDiff (G:\dev\plan-o-clock\node_modules\drizzle-kit\bin.cjs:18201:69)
    at applyPgSnapshotsDiff (G:\dev\plan-o-clock\node_modules\drizzle-kit\bin.cjs:19822:26)

Error is not present on older versions of drizzle-kit

bun db:push
$ bunx drizzle-kit push:pg
drizzle-kit: v0.20.17
drizzle-orm: v0.30.10

No config path provided, using default path
Reading config file 'G:\dev\plan-o-clock\drizzle.config.ts'
[✓] Changes applied

Expected behavior

Please let me know if there is anything else that you need!

Environment & setup

Drizzle Config

import { defineConfig } from "drizzle-kit";

import { env } from "@/env";

export default defineConfig({
  schema: "./src/server/db/schema.ts",
  dialect: "postgresql",
  dbCredentials: {
    url: env.DATABASE_URL,
  },
  tablesFilter: ["plan-o-clock_*"],
});

Drizzle Schema

import { relations, sql } from "drizzle-orm";
import {
  index,
  pgTableCreator,
  serial,
  text,
  varchar,
  timestamp,
  pgEnum,
  date,
  integer,
} from "drizzle-orm/pg-core";

/**
 * This is an example of how to use the multi-project schema feature of Drizzle ORM. Use the same
 * database instance for multiple projects.
 *
 * @see https://orm.drizzle.team/docs/goodies#multi-project-schema
 */
export const createTable = pgTableCreator((name) => `plan-o-clock_${name}`);

export const board = createTable("board", {
  id: serial("id").primaryKey(),
  title: varchar("title").notNull(),
  orgId: varchar("org_id").notNull(),
  imageId: varchar("image_id"),
  imageThumbUrl: text("image_thumb_url"),
  imageFullUrl: text("image_full_url"),
  imageUserName: text("image_user_name"),
  imageLinkHTML: text("image_link_html"),
  createdAt: timestamp("created_at")
    .default(sql`CURRENT_TIMESTAMP`)
    .notNull(),
  updatedAt: timestamp("updated_at", { mode: "date", precision: 3 }).$onUpdate(
    () => new Date(),
  ),
});

export const boardRelations = relations(board, ({ many }) => ({
  lists: many(list),
}));

export const list = createTable(
  "list",
  {
    id: serial("id").primaryKey(),
    title: varchar("title").notNull(),
    order: serial("order").notNull(),
    boardId: integer("board_id")
      .notNull()
      .references(() => board.id),
    createdAt: timestamp("created_at")
      .default(sql`CURRENT_TIMESTAMP`)
      .notNull(),
    updatedAt: timestamp("updated_at", {
      mode: "date",
      precision: 3,
    }).$onUpdate(() => new Date()),
  },
  (list) => ({
    boardIndex: index("board_index").on(list.boardId),
  }),
);

export const listRelations = relations(list, ({ many }) => ({
  cards: many(card),
}));

export const card = createTable(
  "card",
  {
    id: serial("id").primaryKey(),
    title: varchar("title").notNull(),
    order: serial("order").notNull(),
    description: text("description"),
    listId: integer("list_id")
      .notNull()
      .references(() => list.id),
    dueDate: date("due_date").notNull(),
    createdAt: timestamp("created_at")
      .default(sql`CURRENT_TIMESTAMP`)
      .notNull(),
    updatedAt: timestamp("updated_at", {
      mode: "date",
      precision: 3,
    }).$onUpdate(() => new Date()),
  },
  (card) => ({
    listIndex: index("list_index").on(card.listId),
  }),
);

export const actionEnum = pgEnum("action", ["CREATE", "UPDATE", "DELETE"]);

export const entityTypeEnum = pgEnum("entity_type", ["BOARD", "LIST", "CARD"]);

export const auditLog = createTable("audit_log", {
  id: serial("id").primaryKey(),
  orgId: varchar("org_id").notNull(),
  action: actionEnum("action").notNull(),
  entityId: varchar("entity_id").notNull(),
  entityType: entityTypeEnum("entity_type").notNull(),
  entityTitle: varchar("entity_title"),
  userId: varchar("user_id").notNull(),
  userImage: text("user_image"),
  userName: varchar("user_name"),
  createdAt: timestamp("created_at")
    .default(sql`CURRENT_TIMESTAMP`)
    .notNull(),
  updatedAt: timestamp("updated_at", { mode: "date", precision: 3 }).$onUpdate(
    () => new Date(),
  ),
});

export type Board = typeof board.$inferSelect;
export type NewBoard = typeof board.$inferInsert;
export type List = typeof list.$inferSelect;
export type NewList = typeof list.$inferInsert;
export type Card = typeof card.$inferSelect;
export type NewCard = typeof card.$inferInsert;
export type AuditLog = typeof auditLog.$inferSelect;
export type NewAuditLog = typeof auditLog.$inferInsert;

Package.json

{
  "name": "plan-o-clock",
  "version": "0.1.0",
  "private": true,
  "type": "module",
  "scripts": {
    "build": "next build",
    "db:push": "bunx drizzle-kit push:pg",
    "db:studio": "bunx drizzle-kit studio",
    "dev": "next dev",
    "lint": "next lint",
    "start": "next start"
  },
  "dependencies": {
    "@clerk/nextjs": "^4.29.5",
    "@heroicons/react": "^2.1.1",
    "@hookform/resolvers": "^3.3.4",
    "@neondatabase/serverless": "^0.9.1",
    "@planetscale/database": "^1.11.0",
    "@radix-ui/react-accordion": "^1.1.2",
    "@radix-ui/react-dialog": "^1.0.5",
    "@radix-ui/react-icons": "^1.3.0",
    "@radix-ui/react-label": "^2.0.2",
    "@radix-ui/react-popover": "^1.0.7",
    "@radix-ui/react-separator": "^1.0.3",
    "@radix-ui/react-slot": "^1.0.2",
    "@t3-oss/env-nextjs": "^0.7.1",
    "@types/lodash": "^4.17.4",
    "class-variance-authority": "^0.7.0",
    "clsx": "^2.1.0",
    "cmdk": "^1.0.0",
    "date-fns": "^3.6.0",
    "drizzle-kit": "^0.21.4",
    "drizzle-orm": "^0.30.10",
    "lodash": "^4.17.21",
    "lucide-react": "^0.321.0",
    "next": "^14.0.4",
    "next-themes": "^0.3.0",
    "react": "18.2.0",
    "react-day-picker": "^8.10.1",
    "react-dom": "18.2.0",
    "react-hook-form": "^7.49.3",
    "sonner": "^1.4.41",
    "tailwind-merge": "^2.2.1",
    "tailwindcss-animate": "^1.0.7",
    "usehooks-ts": "^3.1.0",
    "vaul": "^0.9.0",
    "zod": "^3.22.4",
    "zustand": "^4.5.0"
  },
  "devDependencies": {
    "@types/eslint": "^8.44.7",
    "@types/node": "^18.17.0",
    "@types/react": "^18.2.37",
    "@types/react-dom": "^18.2.15",
    "@typescript-eslint/eslint-plugin": "^6.11.0",
    "@typescript-eslint/parser": "^6.11.0",
    "autoprefixer": "^10.4.14",
    "eslint": "^8.54.0",
    "eslint-config-next": "^14.0.4",
    "pg": "^8.11.5",
    "postcss": "^8.4.31",
    "prettier": "^3.1.0",
    "prettier-plugin-tailwindcss": "^0.5.7",
    "tailwindcss": "^3.3.5",
    "typescript": "^5.1.6"
  },
  "ct3aMetadata": {
    "initVersion": "7.26.0"
  }
}
cayter commented 5 months ago

This is likely due to drizzle-kit is trying to patch to ensure timestamp(N) and timestamp (N) are seen as no difference. However, this part of the code forgot to filter out the no difference part which led to the bug.

Screenshot 2024-05-28 at 12 39 29 PM
KostarSf commented 5 months ago

Same error when trying to change timestamp's mode from "string" to "date"

aneurycasado commented 5 months ago

Is there an update on this bug? I am running into it as well.

sp88011 commented 5 months ago

Getting a similar error since moving to drizzle-kit 0.22.0 and drizzle-orm 0.31.0

No error in drizzle-kit 0.21.0 and drizzle-orm 0.30.7.

drizzle Error: Cannot use 'in' operator to search for 'enumValues' in undefined
michaeldebetaz commented 5 months ago

Same error when trying to change timestamp's mode from "string" to "date"

Same problem here when changing just one timestamp mode from "string" to "date". Can't do any migration with latest release until it's fixed. I'll downgrade for the time being 👍

BradNut commented 5 months ago

Any update on this? I wanted to use the new full text search in these new versions but sadly I will still need to wait.

AndriiSherman commented 5 months ago

Should be fixed in drizzle-kit@0.22.5

madebyfabian commented 3 months ago

Geting the same as @sp88011 when trying to use the built in geometry. Does anyone still have similar issues?

austinm911 commented 2 months ago

Geting the same as @sp88011 when trying to use the built in geometry. Does anyone still have similar issues?

i reopened this as I'm getting the error with drizzle-zod https://github.com/drizzle-team/drizzle-orm/issues/2890

kaeon commented 4 weeks ago

Same here when i use point (postgress) in tuple mode, not sure if it's drizzle-valibot or drizzle-orm that shows this error

adamszeptycki commented 2 weeks ago

I have the same issue when I'm trying to add a vector column.

embedding: vector("embedding", { dimensions: 1024 }),
adamszeptycki commented 2 weeks ago

Ok - for me it was drizzle-zod commenting it out to generate migrations worked for me. At least I was able to generate schemas.

zxt-tzx commented 2 weeks ago

Ok - for me it was drizzle-zod commenting it out to generate migrations worked for me. At least I was able to generate schemas.

thank you, this is helpful. encountered the same issue

adamszeptycki commented 2 weeks ago

@zxt-tzx For that one table I had to to manually create zod schemas and everything works.