L-Mario564 / drizzle-dbml-generator

Generate DBML markup from your schema defined with Drizzle ORM.
MIT License
154 stars 7 forks source link

Table relations not generated #17

Closed pitzzahh closed 1 month ago

pitzzahh commented 2 months ago

Hi. It generated the dbml and the svg output, however the relations are not present.

I have my schema per file and merged them in an index.ts, does it work with this? or does it need to be in a single schema file?

1 directory, 27 files `

export const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });

const db = drizzle(pool, { schema }); const out = './schema.dbml'; const relational = true;

console.info(pgGenerate({ schema, out, relational }));

export default db;


```bash
"dbml:generate": "bun run -r dotenv/config src/lib/server/db/index.ts && bun dbml:render",
"dbml:render": "bun dbml-renderer -i schema.dbml -o erd.svg"
L-Mario564 commented 1 month ago

The way you have it set up should work. Can you show me a snippet where I can see how you're defining relations?

pitzzahh commented 1 month ago

The way you have it set up should work. Can you show me a snippet where I can see how you're defining relations?

Hi, thanks for the response. It works now, it was my fault that I did not include the relations as an export. I thought it works out of the box. I now created a sub directory containing the table relations and it now generates it properly.

pitzzahh commented 1 month ago
import { pgTable, text, timestamp } from 'drizzle-orm/pg-core';
import { relations } from 'drizzle-orm';
import { employee } from '.';
import { createId } from '@paralleldrive/cuid2';

const appointmentInformation = pgTable('appointment_info', {
        id: text('id')
                .primaryKey()
                .$defaultFn(() => createId()),
        createdAt: timestamp('created_at', { precision: 3 }).defaultNow(),
        updatedAt: timestamp('updated_at', { precision: 3 }).$onUpdate(() => new Date()),
        appointmentDate: timestamp('appointment_date', { precision: 3 }).notNull(),
        lastPromotionDate: timestamp('last_promotion', { precision: 3 }).notNull(),
        employeeId: text('employee_id').notNull()
});

export const appointmentStatusRelations = relations(appointmentInformation, ({ one }) => {
        return {
                employee: one(employee, {
                        fields: [appointmentInformation.employeeId],
                        references: [employee.id]
                })
        };
});

export default appointmentInformation;
pitzzahh commented 1 month ago
import { pgTable, text, timestamp } from 'drizzle-orm/pg-core';
import { relations } from 'drizzle-orm';
import { employee } from '.';
import { createId } from '@paralleldrive/cuid2';

const appointmentInformation = pgTable('appointment_info', {
        id: text('id')
                .primaryKey()
                .$defaultFn(() => createId()),
        createdAt: timestamp('created_at', { precision: 3 }).defaultNow(),
        updatedAt: timestamp('updated_at', { precision: 3 }).$onUpdate(() => new Date()),
        appointmentDate: timestamp('appointment_date', { precision: 3 }).notNull(),
        lastPromotionDate: timestamp('last_promotion', { precision: 3 }).notNull(),
        employeeId: text('employee_id').notNull()
});

export const appointmentStatusRelations = relations(appointmentInformation, ({ one }) => {
        return {
                employee: one(employee, {
                        fields: [appointmentInformation.employeeId],
                        references: [employee.id]
                })
        };
});

export default appointmentInformation;

this was fixed by separating the relations to a new file.