-- Fahrzeuge eines Subunternehmer-Accounts. Kein ERP-Spiegel — die App -- pflegt die Fahrzeuge selbst. -- -- Im Audit-Log und an Lieferungen war `car_id` bislang ohne FK -- geführt (Spalten existieren in 0002/0003/0005). Diese Migration -- zieht die FKs nach. Bestehende NULL-Werte bleiben gültig. -- -- `(account_id, plate)` ist fachlich eindeutig — pro Account keine -- doppelten Kennzeichen. CREATE TABLE cars ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), account_id BIGINT NOT NULL REFERENCES accounts(personalnummer), plate TEXT NOT NULL, active BOOLEAN NOT NULL DEFAULT TRUE ); CREATE UNIQUE INDEX cars_account_plate ON cars(account_id, plate); CREATE INDEX cars_account ON cars(account_id); -- FKs nachziehen — alle Spalten bleiben NULLABLE, weil -- Audit-Einträge der Vor-Cars-Phase keinen car_id haben. ALTER TABLE deliveries ADD CONSTRAINT deliveries_assigned_car_fk FOREIGN KEY (assigned_car_id) REFERENCES cars(id); ALTER TABLE delivery_notes ADD CONSTRAINT delivery_notes_author_car_fk FOREIGN KEY (author_car_id) REFERENCES cars(id); ALTER TABLE scan_audit ADD CONSTRAINT scan_audit_actor_car_fk FOREIGN KEY (actor_car_id) REFERENCES cars(id); -- Seed: zwei Fahrzeuge für Testfahrer (PN 1001) — bewusst zwei, -- damit der Flow "Auswählen ab 2 Autos" testbar ist. INSERT INTO cars (id, account_id, plate, active) VALUES ('77777777-7777-7777-7777-777777777771', 1001, 'BGL-HZ 100', TRUE), ('77777777-7777-7777-7777-777777777772', 1001, 'BGL-HZ 200', TRUE);