L-Mario564 / drizzle-dbml-generator

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

relations missing in sqlite #2

Closed chrissiwaffler closed 1 year ago

chrissiwaffler commented 1 year ago

relations between tables are missing when having a sqlite schema.

I hope this schema is correctly defined in drizzle

Steps to reproduce:

schema.ts:

import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core';

export const tokens = sqliteTable('tokens', {
  pid: text('pid').primaryKey().notNull(),
  assetName: text('asset_name').notNull(),
});

export const vaults = sqliteTable('vaults', {
  name: text('name').primaryKey().notNull(),
  description: text('description').notNull(),
});

export const vaultsTokens = sqliteTable('vaults_tokens', {
  id: text('id').primaryKey().notNull(),
  vaultName: integer('vault_name')
    .notNull()
    .references(() => vaults.name),
  tokenPid: integer('token_pid')
    .notNull()
    .references(() => tokens.pid),
  percentage: integer('percentage').notNull(),
});

dbml.ts:

import {
  generatedDrizzleSchemaPath,
  generatedSchemaDiagramPath,
} from '@/constants/env';
import * as schema from './schema.ts';
import { sqliteGenerate } from 'drizzle-dbml-generator';

const out = generatedSchemaDiagramPath;
const relational = true;

sqliteGenerate({ schema, out, relational });

generated schema.dbml:

table tokens {
  pid text [pk, not null]
  asset_name text [not null]
}

table vaults {
  name text [pk, not null]
  description text [not null]
}

table vaults_tokens {
  id text [pk, not null]
  vault_name integer [not null]
  token_pid integer [not null]
  percentage integer [not null]
}
L-Mario564 commented 1 year ago

If relational is set to true, the generator won't pick up foreign keys but rather variables defined with the relations function. So in you're case, you need to set relational to false to generate correctly.

chrissiwaffler commented 1 year ago

Oh yes, thanks, didn't fully read your docs šŸ˜¬, works now! Many thanks for replying so quickly šŸ™