© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•12mo ago
Mozart's_Ghost

composite primary key migration fail

I am trying to add a composite primary key via a custom migration file. I ran
drizzle-kit --custom --name=recipient-primary-key
drizzle-kit --custom --name=recipient-primary-key
to generate a custom migration file. In that file i have the following sql

-- Step 1: Rename the old recipients table
ALTER TABLE recipients RENAME TO recipients_old;

-- Step 2: Create the new recipients table with a composite primary key
CREATE TABLE recipients (
    subscription_id TEXT NOT NULL,
    contact_id TEXT NOT NULL,
    preferred_time TEXT NOT NULL,
    PRIMARY KEY (subscription_id, contact_id),
    FOREIGN KEY (subscription_id) REFERENCES subscriptions(id) ON UPDATE NO ACTION ON DELETE NO ACTION,
    FOREIGN KEY (contact_id) REFERENCES contacts(id) ON UPDATE NO ACTION ON DELETE NO ACTION
);

-- Step 3: Copy valid data from recipients_old to the new recipients table
INSERT INTO recipients (subscription_id, contact_id, preferred_time)
SELECT subscription_id, contact_id, preferred_time FROM recipients_old
WHERE subscription_id IS NOT NULL AND contact_id IS NOT NULL;

-- Step 4: Drop the old table
DROP TABLE recipients_old;
-- Step 1: Rename the old recipients table
ALTER TABLE recipients RENAME TO recipients_old;

-- Step 2: Create the new recipients table with a composite primary key
CREATE TABLE recipients (
    subscription_id TEXT NOT NULL,
    contact_id TEXT NOT NULL,
    preferred_time TEXT NOT NULL,
    PRIMARY KEY (subscription_id, contact_id),
    FOREIGN KEY (subscription_id) REFERENCES subscriptions(id) ON UPDATE NO ACTION ON DELETE NO ACTION,
    FOREIGN KEY (contact_id) REFERENCES contacts(id) ON UPDATE NO ACTION ON DELETE NO ACTION
);

-- Step 3: Copy valid data from recipients_old to the new recipients table
INSERT INTO recipients (subscription_id, contact_id, preferred_time)
SELECT subscription_id, contact_id, preferred_time FROM recipients_old
WHERE subscription_id IS NOT NULL AND contact_id IS NOT NULL;

-- Step 4: Drop the old table
DROP TABLE recipients_old;


When I run the migration with
drizzle-kit migrate
drizzle-kit migrate
and then inspect my local sqlite binary I only see a
recipients_old
recipients_old
table. It seems only the first statement in the migration file is successful. When I run these commands manually using sqlite3 they work. Not sure how I can go about debugging.
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

Composite Primary Key?
Drizzle TeamDTDrizzle Team / help
16mo ago
reference composite primary key
Drizzle TeamDTDrizzle Team / help
3y ago
Syntax error mysql migration using composite primary key
Drizzle TeamDTDrizzle Team / help
3y ago
Composite primary key, multiple primary keys
Drizzle TeamDTDrizzle Team / help
2y ago