NotLuksus
NotLuksus
Explore posts from servers
DTDrizzle Team
Created by NotLuksus on 8/2/2023 in #help
ReferenceError: Cannot access 'addon' before initialization
This is one of the files in ./schema/
import {
integer,
primaryKey,
sqliteTable,
text,
} from "drizzle-orm/sqlite-core";
import { Entity } from "../abstract/Entity.ts";
import { organisation, rental } from "@/db/index.ts";
import { relations } from "drizzle-orm";

export const addon = sqliteTable("addon", {
...Entity,
name: text("name").notNull(),
description: text("description").notNull(),
price: integer("price").notNull(),
organisationId: text("organisationId").notNull(),
});

export const addonRelations = relations(addon, ({ one, many }) => ({
organisation: one(organisation, {
fields: [addon.organisationId],
references: [organisation.id],
}),
addontsToRentals: many(addonsToRentals),
}));

export const addonsToRentals = sqliteTable(
"addons_to_rentals",
{
addonId: text("addonId")
.notNull()
.references(() => addon.id),
rentalId: text("rentalId")
.notNull()
.references(() => rental.id),
},
t => ({
pk: primaryKey(t.addonId, t.rentalId),
}),
);

export const addonsToRentalsRelations = relations(
addonsToRentals,
({ one }) => ({
addon: one(addon, {
fields: [addonsToRentals.addonId],
references: [addon.id],
}),
rental: one(rental, {
fields: [addonsToRentals.rentalId],
references: [rental.id],
}),
}),
);
import {
integer,
primaryKey,
sqliteTable,
text,
} from "drizzle-orm/sqlite-core";
import { Entity } from "../abstract/Entity.ts";
import { organisation, rental } from "@/db/index.ts";
import { relations } from "drizzle-orm";

export const addon = sqliteTable("addon", {
...Entity,
name: text("name").notNull(),
description: text("description").notNull(),
price: integer("price").notNull(),
organisationId: text("organisationId").notNull(),
});

export const addonRelations = relations(addon, ({ one, many }) => ({
organisation: one(organisation, {
fields: [addon.organisationId],
references: [organisation.id],
}),
addontsToRentals: many(addonsToRentals),
}));

export const addonsToRentals = sqliteTable(
"addons_to_rentals",
{
addonId: text("addonId")
.notNull()
.references(() => addon.id),
rentalId: text("rentalId")
.notNull()
.references(() => rental.id),
},
t => ({
pk: primaryKey(t.addonId, t.rentalId),
}),
);

export const addonsToRentalsRelations = relations(
addonsToRentals,
({ one }) => ({
addon: one(addon, {
fields: [addonsToRentals.addonId],
references: [addon.id],
}),
rental: one(rental, {
fields: [addonsToRentals.rentalId],
references: [rental.id],
}),
}),
);
When I run npx drizzle-kit generate:sqlite I get the error:
ReferenceError: Cannot access 'addon' before initialization
at Object.addon (/Users/notluksus/Work/velocita/db/schema/addon.ts:1:1)
at Object.get [as addon] (/Users/notluksus/Work/velocita/db/schema/addon.ts:14:45)
at Object.get [as addon] (/Users/notluksus/Work/velocita/db/schema/index.ts:10:45)
at Object.get [as addon] (/Users/notluksus/Work/velocita/db/index.ts:16:45)
at Function.entries (<anonymous>)
at Object.extractTablesRelationalConfig (/Users/notluksus/Work/velocita/node_modules/drizzle-orm/alias-72a4082c.cjs:3445:39)
at drizzle (/Users/notluksus/Work/velocita/node_modules/src/better-sqlite3/driver.ts:32:24)
at Object.<anonymous> (/Users/notluksus/Work/velocita/db/index.ts:6:12)
at Module._compile (node:internal/modules/cjs/loader:1226:14)
at Module._compile (/Users/notluksus/Work/velocita/node_modules/drizzle-kit/index.cjs:8604:30)
ReferenceError: Cannot access 'addon' before initialization
at Object.addon (/Users/notluksus/Work/velocita/db/schema/addon.ts:1:1)
at Object.get [as addon] (/Users/notluksus/Work/velocita/db/schema/addon.ts:14:45)
at Object.get [as addon] (/Users/notluksus/Work/velocita/db/schema/index.ts:10:45)
at Object.get [as addon] (/Users/notluksus/Work/velocita/db/index.ts:16:45)
at Function.entries (<anonymous>)
at Object.extractTablesRelationalConfig (/Users/notluksus/Work/velocita/node_modules/drizzle-orm/alias-72a4082c.cjs:3445:39)
at drizzle (/Users/notluksus/Work/velocita/node_modules/src/better-sqlite3/driver.ts:32:24)
at Object.<anonymous> (/Users/notluksus/Work/velocita/db/index.ts:6:12)
at Module._compile (node:internal/modules/cjs/loader:1226:14)
at Module._compile (/Users/notluksus/Work/velocita/node_modules/drizzle-kit/index.cjs:8604:30)
And I don't understand why since the schema doesn't have any self references
32 replies
DTDrizzle Team
Created by NotLuksus on 8/1/2023 in #help
Migrating prisma schema to drizzle
1 replies
DTDrizzle Team
Created by NotLuksus on 4/4/2023 in #help
"Extend" tables by other tables
Hey all, first of all great work with drizzle. Haven't had the time to play around with it so far. But just from how I currently use other orms. I have an AbstractEntity "class" which isn't an actual db table, but shares the common column each table should have e.g. a unique id. Basically like a parent class, but for DB tables. Is it possible to do something similar. Without trying: Can I just create an object using the given types/constructors from drizzle and then spread them? I hope that makes sense
2 replies