Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

drizzle-orm not create table #403

Open
Hiram-Wong opened this issue Oct 22, 2024 · 2 comments
Open

drizzle-orm not create table #403

Hiram-Wong opened this issue Oct 22, 2024 · 2 comments

Comments

@Hiram-Wong
Copy link

import { integer, pgTable, varchar } from 'drizzle-orm/pg-core';
import { PGlite } from '@electric-sql/pglite';
import { drizzle } from 'drizzle-orm/pglite';
import { app } from 'electron';
import { join } from 'path';

const DB_PATH = join(app.getPath('userData'), 'database');
const client = new PGlite(DB_PATH);
const db = drizzle(client);
const setting = pgTable('setting', {
  id: integer().primaryKey().generatedAlwaysAsIdentity(),
  key: varchar({ length: 255 }).notNull(),
  value: varchar({ length: 255 })
});
db.select()
  .from(setting)
  .then((res) => {
    console.log(res);
  });

errorUnhandledPromiseRejectionWarning: error: relation "setting" does not exist

@aleksei-pravdin
Copy link

drizzle does not create tables automatically, you need to use drizzle-kit set migration.

@Venipa
Copy link

Venipa commented Oct 24, 2024

this should do it


import { drizzle } from 'drizzle-orm/pglite'
import { migrate } from 'drizzle-orm/pglite/migrator'

// ...

const databaseInstance = drizzle({
  client: pgClient,
  schema
})
export function migrateAfterClientReady() {
  if (!client.ready) await client.waitReady
    await migrate(databaseInstance, {
      migrationsFolder: path.resolve('src/shared/drizzle/'), // set to your drizzle generated path
      migrationsSchema: path.resolve('src/shared/db/schema'), // set to your schema path
      migrationsTable: '__migrations'
    })
    
  }
  
migrateAfterClientReady();

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants