Is it possible to “catch” the invalid value in MySQL when I get a foreign key constraint error?

ⅰ亾dé卋堺 提交于 2021-01-05 05:39:45

问题


I'm importing a CSV file of ~150k rows into a MySQL (InnoDB) table.

At some point during the import, there's a failure because of a foreign key constraint error.

It shows which column the failure is occuring on:

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails 
(`db`.`table`, CONSTRAINT `fk` FOREIGN KEY (`otherTableId`) REFERENCES `otherTable`
(`otherTableId`))

Is it possible to "catch" what that value is?

I haven't tried anything yet, because I don't know what to try. :P


Edit:

Ahh - so I found the following command in the MySQL documentation:

SHOW ENGINE INNODB STATUS\G

I found the following information from that command:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
2014-01-22 16:27:26 2afaf98b8940 Transaction:
TRANSACTION 85952, ACTIVE 0 sec inserting
mysql tables in use 1, locked 1
4 lock struct(s), heap size 376, 1 row lock(s), undo log entries 1
MySQL thread id 944, OS thread handle 0x2afaf98b8940, query id 66223 System lock
{{QUERY}}
Foreign key constraint fails for table {TABLE INFO}:
,
  CONSTRAINT `fk` FOREIGN KEY (`Id`) REFERENCES `table` (`id`)
Trying to add in child table, in index `fk` tuple:
DATA TUPLE: 2 fields;
 0: len 11; hex 4163636573736f72794964; asc AccessoryId;;
 1: len 4; hex 80000003; asc     ;;

But in parent table `db`.`table`, in index `PRIMARY`,
the closest match we can find is record:
PHYSICAL RECORD: n_fields 5; compact format; info bits 0
 0: len 6; hex 46494c544552; asc FILTER;;
 1: len 6; hex 000000007300; asc     s ;;
 2: len 7; hex fb000001b01476; asc       v;;
 3: len 1; hex 46; asc F;;
 4: len 9; hex 756e646566696e6564; asc undefined;;

What I'm looking for in here is:

Trying to add in child table, in index `fk` tuple:
DATA TUPLE: 2 fields;
 0: len 11; hex 4163636573736f72794964; asc AccessoryId;;
 1: len 4; hex 80000003; asc     ;;

I don't know what the 'asc' means before 'AccessoryId', but that led me to seeing that I wasn't ignoring the first line of the CSV.


Edit 2:

Ahh - I thought for a minute about what the 'asc' stood for, and since there's hex just before it, I assumed it meant "ASCII"

I converted the '4163636573736f72794964' into ASCII and got "AccessoryId", so that was just the HEX value of the text that I was trying to import.


So the answer is:

SHOW ENGINE INNODB STATUS\G

And search for the section titled: LATEST FOREIGN KEY ERROR

You can get your info from there. If anyone knows a better or automated way of retrieving this info, I'll accept it as an answer.


回答1:


Unfortunately there is no better way to get the foreign key errors than from the LATEST FOREIGN KEY ERROR section in SHOW ENGINE INNODB STATUS. The column information is printed as e.g.:

Trying to add in child table, in index `fk` tuple:
DATA TUPLE: 2 fields;
 0: len 11; hex 4163636573736f72794964; asc AccessoryId;;
 1: len 4; hex 80000003; asc     ;;

This is printing the full set of columns from the index (fk), and as you guessed the format is:

<column index>:
  len <length of bytes stored>;
  hex <hex representation of bytes>;
  asc <ascii representation of bytes>;;

Further unfortunately, InnoDB doesn't know enough about how MySQL's column types are stored to give you a reasonable printed representation, so some of the values are a bit "weird" e.g. 80000003 is the hex representation of the bytes stored for the integer 3 (InnoDB internally flips the high bit).



来源:https://stackoverflow.com/questions/21297110/is-it-possible-to-catch-the-invalid-value-in-mysql-when-i-get-a-foreign-key-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!