Input Tokens
24446
Output Tokens
5567
Duration
138424.297
Cost
0
Context
Input
Add a foreign key from completions to enrollments, linking through user_id and course_id to ensure data consistency.
Expected output
ALTER TABLE completions ADD COLUMN course_id INT, ADD CONSTRAINT fk_completions_user_course FOREIGN KEY (user_id, course_id) REFERENCES enrollments(user_id, course_id);
Model output
-- First, add the course_id column to completions (if not already present)
-- ALTER TABLE completions ADD COLUMN course_id INT;
-- Update the course_id column based on the lesson's course_id
-- UPDATE completions c
-- SET course_id = l.course_id
-- FROM lessons l
-- WHERE c.lesson_id = l.lesson_id;
-- Add the composite foreign key constraint
ALTER TABLE completions
ADD CONSTRAINT fk_completions_enrollments
FOREIGN KEY (user_id, course_id) REFERENCES enrollments(user_id, course_id);