I'm hoping I'm just being an idiot here, but I've got a query that truncates a table, then selects all possible combinations of some foreign keys and inserts them to make a profile.
I've got 2 x 2 x 3 x 4 x 7 keys to select, which comes to 336 distinct combinations.
If I run this select, I get 336 rows:
SELECT DISTINCT
`contract_agreement_type`.`id`,
`contract_deposit`.`id`,
`contract_length`.`id`,
`contract_maintenance_type`.`id`,
`contract_mileage`.`id`
FROM `contract_agreement_type`,
`contract_deposit`,
`contract_length`,
`contract_maintenance_type`,
`contract_mileage`
Woohoo, that's great! But when I insert it, I ended up with 672 rows?! To check my maths was right, I added an index on the 5 columns, and when I run it again, I get a duplicate key error:
INSERT INTO `contract_profile`
(
`contract_agreement_type_id`,
`contract_deposit_id`,
`contract_length_id`,
`contract_maintenance_type_id`,
`contract_mileage_id`
)
SELECT DISTINCT
`contract_agreement_type`.`id`,
`contract_deposit`.`id`,
`contract_length`.`id`,
`contract_maintenance_type`.`id`,
`contract_mileage`.`id`
FROM `contract_agreement_type`,
`contract_deposit`,
`contract_length`,
`contract_maintenance_type`,
`contract_mileage`
# Duplicate entry '1-3-24-1-5' for key 'profile'
So the select is fine, but the insert duplicates the data somehow - what have I done wrong?!