drizzle-kit push tells me it found data-loss statements and asks me to confirm to delete tables : /

I don't get why this command wants to delete my tables... All the tables in the image below belongs to my public schema. Could anyone tell me why ? This is basically all I'm doing...
// src\db\index.ts
import { config } from "dotenv";
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";

import * as schema from "@/db/schema";

config({
path:
process.env.NODE_ENV === "production"
? ".env.production"
: ".env.development",
});

export const pool = new Pool({
connectionString: process.env.DATABASE_URL!,
ssl: true,
});

const db = drizzle(pool, {
schema,
logger: true,
casing: "snake_case",
});

export default db;
// src\db\index.ts
import { config } from "dotenv";
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";

import * as schema from "@/db/schema";

config({
path:
process.env.NODE_ENV === "production"
? ".env.production"
: ".env.development",
});

export const pool = new Pool({
connectionString: process.env.DATABASE_URL!,
ssl: true,
});

const db = drizzle(pool, {
schema,
logger: true,
casing: "snake_case",
});

export default db;
// drizzle.config.ts
import { config } from "dotenv";
import { defineConfig } from "drizzle-kit";

config({
path:
process.env.NODE_ENV === "production"
? ".env.production"
: ".env.development",
});

export default defineConfig({
out: "./src/db/migrations",
schema: "./src/db/schema/index.ts",
dialect: "postgresql",
dbCredentials: {
url: process.env.DATABASE_URL!,
},
strict: true,
});
// drizzle.config.ts
import { config } from "dotenv";
import { defineConfig } from "drizzle-kit";

config({
path:
process.env.NODE_ENV === "production"
? ".env.production"
: ".env.development",
});

export default defineConfig({
out: "./src/db/migrations",
schema: "./src/db/schema/index.ts",
dialect: "postgresql",
dbCredentials: {
url: process.env.DATABASE_URL!,
},
strict: true,
});
// src\db\schema\index.ts
export * from "./post";
// src\db\schema\index.ts
export * from "./post";
Solution:
Oh, I think I found why here... https://orm.drizzle.team/docs/drizzle-kit-push#including-tables-schemas-and-extensions Seems that defining your schema name with pgSchema is not enough if you're doing drizzle-kit push because it defaults to public schema...
Jump to solution
4 Replies
jeflopo
jeflopoOP2mo ago
And this...
// src\db\schema\post.ts
import { pgSchema } from "drizzle-orm/pg-core";
import { type InferSelectModel, type InferInsertModel, sql } from "drizzle-orm";
import * as t from "drizzle-orm/pg-core";

export const blogSchema = pgSchema("blog");

const timestamps = {
updatedAt: t.timestamp({ withTimezone: true, mode: "string" }),
createdAt: t
.timestamp({ withTimezone: true, mode: "string" })
.defaultNow()
.notNull(),
deletedAt: t.timestamp({ withTimezone: true, mode: "string" }),
};

export const Post = blogSchema.table(
"Post",
{
id: t.integer().primaryKey().generatedAlwaysAsIdentity(),
title: t.text().notNull(),
slug: t.text().notNull().unique(),
featuredImage: t.text(),
excerpt: t.text().notNull(),
author: t.text().notNull(),
category: t.text().notNull(),
tags: t
.text()
.array()
.notNull()
.default(sql`ARRAY[]::text[]`),
readingTime: t.integer().notNull(),
content: t.text().notNull(),
components: t
.text()
.array()
.notNull()
.default(sql`ARRAY[]::text[]`),
...timestamps,
},
(table) => {
return [
t.uniqueIndex("slug_idx").on(table.slug),
t.index("title_idx").on(table.title),
];
}
);

export type SelectPost = InferSelectModel<typeof Post>;
export type InsertPost = InferInsertModel<typeof Post>;
// src\db\schema\post.ts
import { pgSchema } from "drizzle-orm/pg-core";
import { type InferSelectModel, type InferInsertModel, sql } from "drizzle-orm";
import * as t from "drizzle-orm/pg-core";

export const blogSchema = pgSchema("blog");

const timestamps = {
updatedAt: t.timestamp({ withTimezone: true, mode: "string" }),
createdAt: t
.timestamp({ withTimezone: true, mode: "string" })
.defaultNow()
.notNull(),
deletedAt: t.timestamp({ withTimezone: true, mode: "string" }),
};

export const Post = blogSchema.table(
"Post",
{
id: t.integer().primaryKey().generatedAlwaysAsIdentity(),
title: t.text().notNull(),
slug: t.text().notNull().unique(),
featuredImage: t.text(),
excerpt: t.text().notNull(),
author: t.text().notNull(),
category: t.text().notNull(),
tags: t
.text()
.array()
.notNull()
.default(sql`ARRAY[]::text[]`),
readingTime: t.integer().notNull(),
content: t.text().notNull(),
components: t
.text()
.array()
.notNull()
.default(sql`ARRAY[]::text[]`),
...timestamps,
},
(table) => {
return [
t.uniqueIndex("slug_idx").on(table.slug),
t.index("title_idx").on(table.title),
];
}
);

export type SelectPost = InferSelectModel<typeof Post>;
export type InsertPost = InferInsertModel<typeof Post>;
jeflopo
jeflopoOP2mo ago
No description
Solution
jeflopo
jeflopo2mo ago
Oh, I think I found why here... https://orm.drizzle.team/docs/drizzle-kit-push#including-tables-schemas-and-extensions Seems that defining your schema name with pgSchema is not enough if you're doing drizzle-kit push because it defaults to public schema
jeflopo
jeflopoOP2mo ago
Edit: Yep... that's what was happening. I don't yet properly understand the delete statements... But I guess that is just how the push command internally work (pull > generate alternations diff > apply).
No description

Did you find this page helpful?