MySQL ERROR 1005 (HY000): Can't create table 'schema_name.table_name' (errno: 150)

今天在按照书上的例子学习Yii的时候发现在创建一个有外键的MySQL表的时候总是报以下错误

ERROR 1005 (HY000): Can't create table '.' (errno: 150)

但是仅仅从这个错误消息根本得不到任何对排查错误有意义的信息,找了一些文章大多数是说外键表和原表里声明的字段类型不一致,但是都没有给出怎么查出是这个错误的,后来发现原来可以通过运行"SHOW ENGINE INNODB STATUS;"命令,然后在命令结果中的“LATEST FOREIGN KEY ERROR”部分得到具体的错误消息。如下:

...
mysql> SHOW ENGINE INNODB STATUS\G
*************************** 1. row ***************************
  Type: InnoDB
  Name: 
Status: 
=====================================
110920 23:26:39 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 19 seconds
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 46, signal count 46
Mutex spin waits 0, rounds 260, OS waits 13
RW-shared spins 58, OS waits 29; RW-excl spins 5, OS waits 4
------------------------
LATEST FOREIGN KEY ERROR
------------------------
110920 23:26:33 Error in foreign key constraint of table trackstar_test/tbl_project_user_role:
foreign key (`project_id`) references `tbl_project` (`id`) on delete cascade on update cascade
) engine InnoDB:
Cannot resolve table name close to:
 (`id`) on delete cascade on update cascade
) engine InnoDB
------------
TRANSACTIONS
------------
Trx id counter 0 6375
Purge done for trx's n:o < 0 6353 undo n:o < 0 0
History list length 13
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0 0, not started, process no 755, OS thread id 2991618928
MySQL thread id 42, query id 383 localhost root
SHOW ENGINE INNODB STATUS
--------
FILE I/O
--------
I/O thread 0 state: waiting for i/o request (insert buffer thread)
I/O thread 1 state: waiting for i/o request (log thread)
I/O thread 2 state: waiting for i/o request (read thread)
I/O thread 3 state: waiting for i/o request (write thread)
Pending normal aio reads: 0, aio writes: 0,
 ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
Pending flushes (fsync) log: 0; buffer pool: 0
36 OS file reads, 281 OS file writes, 177 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.05 writes/s, 0.05 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2,
0 inserts, 0 merged recs, 0 merges
Hash table size 34679, node heap has 1 buffer(s)
0.00 hash searches/s, 1.16 non-hash searches/s
---
LOG
---
Log sequence number 0 1774918
Log flushed up to   0 1774918
Last checkpoint at  0 1772796
0 pending log writes, 0 pending chkp writes
128 log i/o's done, 0.05 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 17596386; in additional pool allocated 960512
Dictionary memory allocated 58096
Buffer pool size   512
Free buffers       468
Database pages     43
Modified db pages  10
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages read 35, created 8, written 296
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
1 read views open inside InnoDB
Main thread process no. 755, id 2955570032, state: sleeping
Number of rows inserted 60, updated 0, deleted 0, read 159
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================



你可能感兴趣的:(MySQL ERROR 1005 (HY000): Can't create table 'schema_name.table_name' (errno: 150))