Re: Error Code 1215: “Cannot add foreign key constraint”
Posted by: Arthur Chan
Date: January 17, 2019 09:55PM

Okay, to get the at the error logged by InnoDB, this is what I did:
> SHOW ENGINE INNODB STATUS;
... and this is the logged error, note in particular the
----------------------
LATEST FOREIGN KEY ERROR
----------------------
which hangs on this exception

Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.

Here is the complete error log for the single transaction from InnoDB
Note that Company_ID is the primary key in the table Company.

 =====================================
 2019-01-18 11:36:06 0x3330 INNODB MONITOR OUTPUT
 =====================================
 Per second averages calculated from the last 40 seconds
 -----------------
 BACKGROUND THREAD
 -----------------
 srv_master_thread loops: 76 srv_active, 0 srv_shutdown, 43930 srv_idle
 srv_master_thread log flush and writes: 0
 ----------
 SEMAPHORES
 ----------
 OS WAIT ARRAY INFO: reservation count 565
 OS WAIT ARRAY INFO: signal count 565
 RW-shared spins 16, rounds 27, OS waits 11
 RW-excl spins 31, rounds 197, OS waits 3
 RW-sx spins 0, rounds 0, OS waits 0
 Spin rounds per wait: 1.69 RW-shared, 6.35 RW-excl, 0.00 RW-sx
 ------------------------
 LATEST FOREIGN KEY ERROR
 ------------------------
 2019-01-18 11:35:31 0x3330 Error in foreign key constraint of table iamrasdv/test123:
 FOREIGN KEY (Company_ID) REFERENCES Company(Company_ID)
 )   ENGINE=INNODB AUTO_INCREMENT= 1 CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci:
 Cannot find an index in the referenced table where the
 referenced columns appear as the first columns, or column types
 in the table and the referenced table do not match for constraint.
 Note that the internal storage type of ENUM and SET changed in
 tables created with >= InnoDB-4.1.12, and such columns in old tables
 cannot be referenced by such columns in new tables.
 Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-foreign-key-constraints.html for correct foreign key definition.
 ------------
 TRANSACTIONS
 ------------
 Trx id counter 119848
 Purge done for trx's n:o < 119848 undo n:o < 0 state: running but idle
 History list length 0
 LIST OF TRANSACTIONS FOR EACH SESSION:
 ---TRANSACTION 283247338494640, not started
 0 lock struct(s), heap size 1136, 0 row lock(s)
 ---TRANSACTION 283247338493760, not started
 0 lock struct(s), heap size 1136, 0 row lock(s)
 --------
 FILE I/O
 --------
 I/O thread 0 state: wait Windows aio (insert buffer thread)
 I/O thread 1 state: wait Windows aio (log thread)
 I/O thread 2 state: wait Windows aio (read thread)
 I/O thread 3 state: wait Windows aio (read thread)
 I/O thread 4 state: wait Windows aio (read thread)
 I/O thread 5 state: wait Windows aio (read thread)
 I/O thread 6 state: wait Windows aio (write thread)
 I/O thread 7 state: wait Windows aio (write thread)
 I/O thread 8 state: wait Windows aio (write thread)
 I/O thread 9 state: wait Windows aio (write thread)
 Pending normal aio reads: [0, 0, 0, 0] , aio writes: [0, 0, 0, 0] ,
  ibuf aio reads:, log i/o's:, sync i/o's:
 Pending flushes (fsync) log: 0; buffer pool: 0
 3032 OS file reads, 7659 OS file writes, 1993 OS fsyncs
 9.85 reads/s, 16384 avg bytes/read, 6.77 writes/s, 2.17 fsyncs/s
 -------------------------------------
 INSERT BUFFER AND ADAPTIVE HASH INDEX
 -------------------------------------
 Ibuf: size 1, free list len 0, seg size 2, 0 merges
 merged operations:
  insert 0, delete mark 0, delete 0
 discarded operations:
  insert 0, delete mark 0, delete 0
 Hash table size 2267, node heap has 1 buffer(s)
 Hash table size 2267, node heap has 1 buffer(s)
 Hash table size 2267, node heap has 1 buffer(s)
 Hash table size 2267, node heap has 1 buffer(s)
 Hash table size 2267, node heap has 1 buffer(s)
 Hash table size 2267, node heap has 1 buffer(s)
 Hash table size 2267, node heap has 1 buffer(s)
 Hash table size 2267, node heap has 1 buffer(s)
 0.02 hash searches/s, 14.25 non-hash searches/s
 ---
 LOG
 ---
 Log sequence number          224859913
 Log buffer assigned up to    224859913
 Log buffer completed up to   224859913
 Log written up to            224859913
 Log flushed up to            224859913
 Added dirty pages up to      224859913
 Pages flushed up to          224859913
 Last checkpoint at           224859913
 4135 log i/o's done, 2.77 log i/o's/second
 ----------------------
 BUFFER POOL AND MEMORY
 ----------------------
 Total large memory allocated 8585216
 Dictionary memory allocated 539595
 Buffer pool size   512
 Free buffers       260
 Database pages     244
 Old database pages 0
 Modified db pages  0
 Pending reads      0
 Pending writes: LRU 0, flush list 0, single page 0
 Pages made young 0, not young 0
 0.00 youngs/s, 0.00 non-youngs/s
 Pages read 3007, created 496, written 3100
 9.85 reads/s, 0.17 creates/s, 3.72 writes/s
 Buffer pool hit rate 836 / 1000, young-making rate 0 / 1000 not 0 / 1000
 Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
 LRU len: 244, unzip_LRU len: 0
 I/O sum[607]:cur[0], unzip sum[0]:cur[0]
 --------------
 ROW OPERATIONS
 --------------
 0 queries inside InnoDB, 0 queries in queue
 0 read views open inside InnoDB
 Process ID=5140, Main thread ID=0000000000001CB8 , state=sleeping
 Number of rows inserted 2623, updated 673, deleted 181, read 14659
 0.52 inserts/s, 0.00 updates/s, 0.52 deletes/s, 1.17 reads/s
 ----------------------------
 END OF INNODB MONITOR OUTPUT
 ============================
 

Options: ReplyQuote


Subject
Written By
Posted
Re: Error Code 1215: “Cannot add foreign key constraint”
January 17, 2019 09:55PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.