Push with schema in separate files
I have my schemas setup something like customer.ts vendor.ts inventory.ts public.ts
In my config I have schema: "./app/lib/data/db/schema/*"
In public.ts I am creating tables with pgTable but in the non public ts files I am creating a schema with export const customer = pgSchema("customer") and then creating tables with export const customerTable = customer.table("customer", {fieilds here})
When I run drizzle-kit push:pg all that is created is anything in the public file and pgTable
What am I missing?
9 Replies
Tried it with the simple example in docs too and it still doesn’t work ‘import { serial, text, pgTable, pgSchema } from "drizzle-orm/pg-core";
export const mySchema = pgSchema("my_schema")
export const mySchemaUsers = mySchema.table('users', {
id: serial('id').primaryKey(),
name: text('name'),
});’
@A_Dev I do the same thing
database/schema/index.ts
database/index.ts
looks something like this:
tsconfig.json
Basic server side useage:
The setup docs are a mess, so it took a lot of trial and errorYeah they are. Much appreciated I’ll give that a shot!
@Scarcer why redeclare the schemas and relations as additional objects? surely using a barrel export would achieve the same effect and have far less code on your part eg
@Scarcer im asking from a point of best practices and effeciency. I am genuinely curious regarding the thought process behind your implementation
@Darth_Fuzzy The reason is lame. Simply, linters in vs code don't track that implementation as well.
Waking up this post again!
Let's say if I want to have a schema for auth, audit and public.
If I try to delegate this to different files I get:
ReferenceError: Cannot access 'authSchema' before initialization
The error disappears if I put all the table definitions in the same file as the authSchema.
How would I approach this problem in a structured way?