© 2026 Hedgehog Software, LLC

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

Mysterious Postgres Schema Issue

I have the following
schema.ts
schema.ts
file:
import { pgTable, uuid } from 'drizzle-orm/pg-core';

export const FacultyTable = pgTable(
    'faculty',
    {
        id: uuid('id').primaryKey().defaultRandom()
    }
);

export const CourseTable = pgTable(
    'courses',
    {
        id: uuid('id').primaryKey().defaultRandom(),
        bId: uuid('bId').references(() => FacultyTable.id).notNull()
    }
);
import { pgTable, uuid } from 'drizzle-orm/pg-core';

export const FacultyTable = pgTable(
    'faculty',
    {
        id: uuid('id').primaryKey().defaultRandom()
    }
);

export const CourseTable = pgTable(
    'courses',
    {
        id: uuid('id').primaryKey().defaultRandom(),
        bId: uuid('bId').references(() => FacultyTable.id).notNull()
    }
);


To generate the migration sql I run the following:
> yarn drizzle-kit generate:pg
> yarn drizzle-kit generate:pg


And then to complete the migration I run this:
import "dotenv/config";
import { drizzle } from "drizzle-orm/postgres-js";
import { migrate } from "drizzle-orm/postgres-js/migrator";
const postgres = require("postgres");

const migrationClient = postgres(process.env.DATABASE_URL as string, { max: 1 });

async function main() {
    await migrate(drizzle(migrationClient), {
        migrationsFolder: "./src/db/migrations"
    });

    await migrationClient.end();
}

main();
import "dotenv/config";
import { drizzle } from "drizzle-orm/postgres-js";
import { migrate } from "drizzle-orm/postgres-js/migrator";
const postgres = require("postgres");

const migrationClient = postgres(process.env.DATABASE_URL as string, { max: 1 });

async function main() {
    await migrate(drizzle(migrationClient), {
        migrationsFolder: "./src/db/migrations"
    });

    await migrationClient.end();
}

main();


This all works exactly as I would expect, and opening drizzle studio I see the correct columns.
But, for some reason when I switch the uuid from aId to bId it stops working.
In fact, everything I've tried changing it to has resulted in the same error:
PostgresError: column "bId" referenced in foreign key constraint does not exist
PostgresError: column "bId" referenced in foreign key constraint does not exist

I don't see any errors in the SQL code generated but i can send that if any of you think it would be useful. Additionally, I can send my dirzzle.config.ts, package.json, and a photo of my file structure.

Update: I just created a completely new project and was only partially able to reproduce the issue. It doesn't work with aId either now.
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Postgres Schema (namespace)
Drizzle TeamDTDrizzle Team / help
2y ago
postgres schema indexes INCLUDE
Drizzle TeamDTDrizzle Team / help
2y ago
Postgres Schema (namespace) + Drizzle
Drizzle TeamDTDrizzle Team / help
2y ago
postgres push issue
Drizzle TeamDTDrizzle Team / help
3y ago