Mozart's_Ghost
Mozart's_Ghost
Explore posts from servers
DTDrizzle Team
Created by Mozart's_Ghost on 2/11/2025 in #help
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 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 and then inspect my local sqlite binary I only see a 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.
1 replies
TTCTheo's Typesafe Cult
Created by Mozart's_Ghost on 8/31/2024 in #questions
Unexpected useQuery behavior
I am using the t3 stack on a personal project. I have a client component that calls a useQuery hook from from ~trpc/react . I can see in my server logs that the procedure that is called from this use of useQuery is getting called on some interval. Is this default behavior? Can I configure this somewhere? Thanks in advance.
3 replies