-- Fix tickets / tickets_logs primary keys for SAT ticket creation.
-- Error: Field 'id' doesn't have a default value
-- Cause: live DB used composite PK (id, user_id, ticket_type_id, status_id) without AUTO_INCREMENT.
-- Fix: single AUTO_INCREMENT primary key on id, restore foreign keys.

ALTER TABLE tickets_logs DROP FOREIGN KEY tickets_tickets_logs;

ALTER TABLE tickets DROP FOREIGN KEY tickets_status_tickets;
ALTER TABLE tickets DROP FOREIGN KEY tickets_type_tickets;
ALTER TABLE tickets DROP FOREIGN KEY users_tickets;

ALTER TABLE tickets DROP PRIMARY KEY;
ALTER TABLE tickets MODIFY id INT NOT NULL AUTO_INCREMENT PRIMARY KEY;

ALTER TABLE tickets_logs DROP PRIMARY KEY;
ALTER TABLE tickets_logs MODIFY id INT NOT NULL AUTO_INCREMENT PRIMARY KEY;

ALTER TABLE tickets
  ADD CONSTRAINT tickets_status_tickets
    FOREIGN KEY (status_id) REFERENCES tickets_status (status_id),
  ADD CONSTRAINT tickets_type_tickets
    FOREIGN KEY (ticket_type_id) REFERENCES tickets_type (type_id),
  ADD CONSTRAINT users_tickets
    FOREIGN KEY (user_id) REFERENCES users (user_id);

ALTER TABLE tickets_logs
  ADD CONSTRAINT tickets_tickets_logs
    FOREIGN KEY (ticket_id) REFERENCES tickets (id);
