© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
31 replies
NotLuksus

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
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
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Error: Cannot access 'interactionsRelations' before initialization
Drizzle TeamDTDrizzle Team / help
17mo ago
Reference Error: Cannot access "..." before initialization
Drizzle TeamDTDrizzle Team / help
2y ago
Cannot Access Primary Key on Table
Drizzle TeamDTDrizzle Team / help
3y ago
cannot generate migrations, getting a weird ReferenceError I don't know how to debug
Drizzle TeamDTDrizzle Team / help
2y ago