PostgresError: Relation X does not exist - After reorganizing schema

Hi guys, I have two tables (team and team_members) , which have a relation. All my schema was in one file and I decided to split it into multiple to make it cleaner. I ran the drizzle-kit generate:pg command with success, my app starts and does migration with success. But when I try to insert a new team_member it tells me that the relation does not exists. I find that odd, I also double checked I didn't delete anything. Any clue ? It's very late for me, perhaps i'm just missing something super obvious...
7 Replies
Cyber Grandma
Cyber GrandmaOP14mo ago
PostgresError: relation "team_members" does not exist
team.ts:
import { TeamMember, teamMembers } from './team-member';

export const teams = pgTable('teams', {
id: serial('id').primaryKey(),
name: varchar('name', { length: 24 }).notNull().unique(),
logoUrl: varchar('logo_url', { length: 256 }),
maxMembers: integer('max_members').notNull().default(5),
createdAt: date('created_at').notNull().defaultNow()
});

export type Team = typeof teams.$inferSelect;
export type NewTeam = typeof teams.$inferInsert;

export const teamRelations = relations(teams, ({ many }) => ....

export type TeamWithMembers = Team & { members: TeamMember[] };
import { TeamMember, teamMembers } from './team-member';

export const teams = pgTable('teams', {
id: serial('id').primaryKey(),
name: varchar('name', { length: 24 }).notNull().unique(),
logoUrl: varchar('logo_url', { length: 256 }),
maxMembers: integer('max_members').notNull().default(5),
createdAt: date('created_at').notNull().defaultNow()
});

export type Team = typeof teams.$inferSelect;
export type NewTeam = typeof teams.$inferInsert;

export const teamRelations = relations(teams, ({ many }) => ....

export type TeamWithMembers = Team & { members: TeamMember[] };
team-member.ts
import { getTeam, teams } from './team';

export const teamMembers = pgTable(
'team_members',
{
teamId: integer('team_id')
.notNull()
.references(() => teams.id),
userId: varchar('user_id').notNull(),
joinedAt: date('joined_at').notNull().defaultNow()
},
(table) => {
return {
pk: primaryKey(table.teamId, table.userId)
};
}
);

export const teamMemberRelations = relations(teamMembers, ({ one }) => ...

export type TeamMember = typeof teamMembers.$inferSelect;
export type NewTeamMember = typeof teamMembers.$inferInsert;
import { getTeam, teams } from './team';

export const teamMembers = pgTable(
'team_members',
{
teamId: integer('team_id')
.notNull()
.references(() => teams.id),
userId: varchar('user_id').notNull(),
joinedAt: date('joined_at').notNull().defaultNow()
},
(table) => {
return {
pk: primaryKey(table.teamId, table.userId)
};
}
);

export const teamMemberRelations = relations(teamMembers, ({ one }) => ...

export type TeamMember = typeof teamMembers.$inferSelect;
export type NewTeamMember = typeof teamMembers.$inferInsert;
Angelelz
Angelelz14mo ago
Make sure you are importing both files and putting them in the schema object you pass to drizzle
Cyber Grandma
Cyber GrandmaOP14mo ago
Yup, I have a schema.ts file right next to the other two:
export * from './team';
export * from './team-member';
export * from './team';
export * from './team-member';
Then in my db/index.ts file:
import * as schema from './schema/schema';
export type Schema = typeof schema;

...
await migrate(drizzle(migrationClient, { schema }), { migrationsFolder: 'drizzle' });

...

const db: PostgresJsDatabase<Schema> = drizzle(queryClient, { schema });
import * as schema from './schema/schema';
export type Schema = typeof schema;

...
await migrate(drizzle(migrationClient, { schema }), { migrationsFolder: 'drizzle' });

...

const db: PostgresJsDatabase<Schema> = drizzle(queryClient, { schema });
Oh wait ? In my drizzle folder I have this migration sql file:
DROP TABLE "team_members";--> statement-breakpoint
DROP TABLE "teams";
DROP TABLE "team_members";--> statement-breakpoint
DROP TABLE "teams";
Angelelz
Angelelz14mo ago
lol So you probably didn't update your config and ran a migration?
Cyber Grandma
Cyber GrandmaOP14mo ago
I'm gonna delete the folder, re-run generate and try, one sec Oh no I forgot about PPTheClown
Angelelz
Angelelz14mo ago
Understandable
Cyber Grandma
Cyber GrandmaOP14mo ago
It's my first time using drizzle sorry for that lol Works like a charm now nice
Want results from more Discord servers?
Add your server