I'm sorry but I don't understand the error in the object when I add data
in the next table:
CREATE TABLE recipeingredients
(
recipe_id INT(10) UNSIGNED NOT NULL,
subfoodgroup_id INT(10) UNSIGNED NOT NULL,
keepingredient ENUM ("Show","Hide"),
PRIMARY KEY (recipe_id, subfoodgroup_id),
INDEX (recipe_id),
FOREIGN KEY (recipe_id) REFERENCES recipes (recipe_id),
INDEX (subfoodgroup_id),
FOREIGN KEY (subfoodgroup_id) REFERENCES subfoodgroups (subfoodgroup_id)
)
ENGINE=INNODB;
INSERT INTO `recipeingredients` (`recipe_id`, `subfoodgroup_id`, `keepingredient`) VALUES ('5', '413', 'Show');
Obviously there's a key 5 and a key 413 in the foreign tables.
I'm sorry but I don't understand the error in the object when I add data
in the next table:
CREATE TABLE recipeingredients
(
recipe_id INT(10) UNSIGNED NOT NULL,
subfoodgroup_id INT(10) UNSIGNED NOT NULL,
keepingredient ENUM ("Show","Hide"),
PRIMARY KEY (recipe_id, subfoodgroup_id),
INDEX (recipe_id),
FOREIGN KEY (recipe_id) REFERENCES recipes (recipe_id),
INDEX (subfoodgroup_id),
FOREIGN KEY (subfoodgroup_id) REFERENCES subfoodgroups (subfoodgroup_id)
)
ENGINE=INNODB;
INSERT INTO `recipeingredients` (`recipe_id`, `subfoodgroup_id`, `keepingredient`) VALUES ('5', '413', 'Show');
Obviously there's a key 5 and a key 413 in the foreign tables.
And, according to the error message, there's already an entry in your recipeingredients table with recipe_id of 5 and subfoodgroup_id of 413.
As for your INSERT, you could always
INSERT INTO recipeingredients (recipe_id,subfoodgroup_id,keepingredient)
VALUES (5, 413, 'Show')
ON DUPLICATE KEY UPDATE keepingredient = 'Show';
You have said your primary key is the combination of recipe_id and subfoodgroup_id, which means you can only have one of each combination
of recipe_id and subfoodgroup_id in the recipeingredients table.
So you can't have two rows with recipe_id = 5 and subfoodgroup_id = 413,
no matter what the value keepingredient is set to.
Of course you need the recipe_id in the recipes and the subfoodgroup_id
in subfoodgroups, if you are missing a value in those tables when you
try to inser something into recipeingredients table, then you will get another error message than the 1062.
If you want the row to be updated if it already is there, then you can use
INSERT INTO recipeingredients (recipe_id, subfoodgroup_id,
keepingredient) VALUES(5, 413, 'Show') ON DUPLICATE KEY UPDATE keepingredient='Show'
If you think that the value shouldn't be changed, then ignore the error
you got when you made your original duplicate insert (without the on duplicate...).
Like what I said above I started with a empty table and I started to
import data from a *.csv in phpmyadmin and I had immediately the error
so the import didn't work...
If I don't recall it wrong, phpmyadmin uses transactions when using
innodb tables, so if you have an error while importing, it will rollback
the transaction and those the table will be empty when you look at it.
I do suggest you take a look at your csv file and I'm quite sure you
will find two entries with the recipe_id=5 and subfoodgroup_id=413.
Suggest you load it in LibreOffice, sort it first on the recipe_id
column and then the subfoodgroup_id column and you will easilly find
your duplicates.
I don't know why I have a lot of ,,,,, I happened just with this file, I just tried with:
If I don't recall it wrong, phpmyadmin uses transactions when using
innodb tables, so if you have an error while importing, it will
rollback the transaction and those the table will be empty when you
look at it.
If I force the error I can upload a single record but it doesn't work on *.csv import... :\
I do suggest you take a look at your csv file and I'm quite sure you
will find two entries with the recipe_id=5 and subfoodgroup_id=413.
Suggest you load it in LibreOffice, sort it first on the recipe_id
column and then the subfoodgroup_id column and you will easilly find
your duplicates.
My *.csv file:
,,,,,,,,,,,,,,,,,,1,52,Show
,,,,,,,,,,,,,,,,,,1,54,Show
,,,,,,,,,,,,,,,,,,1,35,Show
,,,,,,,,,,,,,,,,,,1,37,Hide
,,,,,,,,,,,,,,,,,,1,39,Hide
,,,,,,,,,,,,,,,,,,1,34,Show
I don't know why I have a lot of ,,,,, I happened just with this file, I just tried with:
1,52,Show
1,54,Show
1,35,Show
1,37,Hide
1,39,Hide
1,34,Show
It works (I tried it on import area of phpmyadmin), maybe there's
something wrong on my *.ods file or when I save it as *.csv.
sed 's/,*\([0-9].*\)/\1/' -i faultyfile.csv
this will remove the leading ,
if you want to remove the duplicates you can run
sort -u faultyfile.csv > noduplicates.csv
of course this is how you do it in the worlds most used OS or an Unix,
if you happen to have that Redmond based company's OS, then you need to install a lot of extra stuff.
Then you solved your issue.
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 63 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 492975:37:05 |
| Calls: | 840 |
| Files: | 1,301 |
| D/L today: |
16 files (28,385K bytes) |
| Messages: | 264,959 |