new to drizzle, need help with relations

I'm new to drizzle, can anyone explain why this code errors
// Example model schema from the Drizzle docs
// https://orm.drizzle.team/docs/sql-schema-declaration

import { create } from "domain";
import { sql, relations } from "drizzle-orm";
import {
index,
pgEnum,
pgTableCreator,
primaryKey,
serial,
timestamp,
varchar,
} from "drizzle-orm/pg-core";

/**
* This is an example of how to use the multi-project schema feature of Drizzle ORM. Use the same
* database instance for multiple projects.
*
* @see https://orm.drizzle.team/docs/goodies#multi-project-schema
*/
export const createTable = pgTableCreator((name) => `loggy_${name}`);

export const users = createTable("users", {
id: varchar("id").primaryKey(),
username: varchar("username", { length: 255 }).notNull(),
});

export const usersRelations = relations(users, ({ many }) => ({
usersToGuilds: many(usersToGuilds),
}));

export const guilds = createTable("guilds", {
id: varchar("id").primaryKey(),
name: varchar("name", { length: 255 }).notNull(),
});

export const usersToGuilds = createTable(
"users_to_guilds",
{
userId: varchar("user_id")
.notNull()
.references(() => users.id),
guildId: varchar("guild_id")
.notNull()
.references(() => guilds.id),
},
(t) => ({
pk: primaryKey({ columns: [t.userId, t.guildId] }),
}),
);

export const guildsRelations = relations(guilds, ({ many }) => ({
usersToGuilds: many(usersToGuilds),
}));

export const messages = createTable("messages", {
id: varchar("id").primaryKey(),
content: varchar("content", { length: 4000 }).notNull(),
user_id: varchar("user_id", { length: 255 })
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
guild_id: varchar("guild_id", { length: 255 })
.notNull()
.references(() => guilds.id, { onDelete: "cascade" }),
});

export const messagesRelations = relations(messages, ({ one }) => ({
user: one(users),
guild: one(guilds),
}));
// Example model schema from the Drizzle docs
// https://orm.drizzle.team/docs/sql-schema-declaration

import { create } from "domain";
import { sql, relations } from "drizzle-orm";
import {
index,
pgEnum,
pgTableCreator,
primaryKey,
serial,
timestamp,
varchar,
} from "drizzle-orm/pg-core";

/**
* This is an example of how to use the multi-project schema feature of Drizzle ORM. Use the same
* database instance for multiple projects.
*
* @see https://orm.drizzle.team/docs/goodies#multi-project-schema
*/
export const createTable = pgTableCreator((name) => `loggy_${name}`);

export const users = createTable("users", {
id: varchar("id").primaryKey(),
username: varchar("username", { length: 255 }).notNull(),
});

export const usersRelations = relations(users, ({ many }) => ({
usersToGuilds: many(usersToGuilds),
}));

export const guilds = createTable("guilds", {
id: varchar("id").primaryKey(),
name: varchar("name", { length: 255 }).notNull(),
});

export const usersToGuilds = createTable(
"users_to_guilds",
{
userId: varchar("user_id")
.notNull()
.references(() => users.id),
guildId: varchar("guild_id")
.notNull()
.references(() => guilds.id),
},
(t) => ({
pk: primaryKey({ columns: [t.userId, t.guildId] }),
}),
);

export const guildsRelations = relations(guilds, ({ many }) => ({
usersToGuilds: many(usersToGuilds),
}));

export const messages = createTable("messages", {
id: varchar("id").primaryKey(),
content: varchar("content", { length: 4000 }).notNull(),
user_id: varchar("user_id", { length: 255 })
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
guild_id: varchar("guild_id", { length: 255 })
.notNull()
.references(() => guilds.id, { onDelete: "cascade" }),
});

export const messagesRelations = relations(messages, ({ one }) => ({
user: one(users),
guild: one(guilds),
}));
2 Replies
StarNumber
StarNumberOP4mo ago
(the error is Error: There is not enough information to infer relation "__public__.guilds.usersToGuilds") Nevermind, I fixed it :)
StarNumber
StarNumberOP4mo ago
No description
Want results from more Discord servers?
Add your server