473,379 Members | 1,216 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Can't create table (errno: 150) - Foreign Key Constraints

2
I have read quite a bit about this error but I have yet to find the solution to my problem.

I am trying to execute the following mysql statement:
Expand|Select|Wrap|Line Numbers
  1. alter table line_items 
  2.                add constraint fk_line_item_products 
  3.                foreign key  (product_id) references products(id)
  4.  
I have also tried the following statement with the same result:
Expand|Select|Wrap|Line Numbers
  1. alter table line_items 
  2.                add constraint  fk_line_item_products 
  3.                foreign key product_idx(product_id) references products(id)
  4.  
which produces the following error message
Can't create table './depot_development/#sql_1.frm' (errno: 150)

Research to this point has clued me into the following where source field is line_items(product_id) and reference field is product(id):

1) type of foreign key source and reference fields must be identical
check: both set to int(11)

2) both source and reference fields must be unsigned
check: both fields are unsigned

3) source field must be indexed
check: product_id is indexed by product_idx

4) both tables must be InnoDB
check: both table InnoDB

Here is a dump of the two tables:

Expand|Select|Wrap|Line Numbers
  1. # Dump of table line_items
  2. # ------------------------------------------------------------
  3.  
  4. CREATE TABLE `line_items` (
  5.   `id` int(11) unsigned NOT NULL auto_increment,
  6.   `product_id` int(11) unsigned NOT NULL default '0',
  7.   `order_id` int(11) NOT NULL default '0',
  8.   `quantity` int(11) NOT NULL default '0',
  9.   `total_price` decimal(8,2) NOT NULL default '0.00',
  10.   PRIMARY KEY  (`id`),
  11.   KEY `product_idx` (`product_id`)
  12. ) TYPE=InnoDB;
  13.  
  14.  
  15.  
  16. # Dump of table products
  17. # ------------------------------------------------------------
  18.  
  19. CREATE TABLE `products` (
  20.   `id` int(11) unsigned NOT NULL auto_increment,
  21.   `title` varchar(255) default NULL,
  22.   `description` text,
  23.   `image_url` varchar(255) default NULL,
  24.   `price` decimal(8,2) default '0.00',
  25.   PRIMARY KEY  (`id`)
  26. ) TYPE=InnoDB;
  27.  
Here is the InnoDB monitor output:

Expand|Select|Wrap|Line Numbers
  1. RW-shared spins 95, OS waits 46; RW-excl spins 1, OS waits 0
  2. ------------------------
  3. LATEST FOREIGN KEY ERROR
  4. ------------------------
  5. 090306 10:58:32 Error in foreign key constraint of table `depot_development/#sql-126_1`:
  6.  
  7.                foreign key  (product_id) references products(id):
  8. Cannot find an index in the referenced table where the
  9. referenced columns appear as the first columns, or column types
  10. in the table and the referenced table do not match for constraint.
  11. See http://www.innodb.com/ibman.php for correct foreign key definition.
  12. ------------
  13. TRANSACTIONS
  14. ------------
  15. Trx id counter 0 3697
  16. Purge done for trx's n:o < 0 3686 undo n:o < 0 0
  17. Total number of lock structs in row lock hash table 0
  18. LIST OF TRANSACTIONS FOR EACH SESSION:
  19. ---TRANSACTION 0 3696, not started, OS thread id 25332736
  20. MySQL thread id 1, query id 744 localhost root
  21. show innodb status
  22. --------
  23. FILE I/O
  24. --------
  25. I/O thread 0 state: waiting for i/o request (insert buffer thread)
  26. I/O thread 1 state: waiting for i/o request (log thread)
  27. I/O thread 2 state: waiting for i/o request (read thread)
  28. I/O thread 3 state: waiting for i/o request (write thread)
  29. Pending normal aio reads: 0, aio writes: 0,
  30.  ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
  31. Pending flushes (fsync) log: 0; buffer pool: 0
  32. 65 OS file reads, 836 OS file writes, 471 OS fsyncs
  33. 0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
  34. -------------------------------------
  35. INSERT BUFFER AND ADAPTIVE HASH INDEX
  36. -------------------------------------
  37. Ibuf for space 0: size 1, free list len 0, seg size 2,
  38. 0 inserts, 0 merged recs, 0 merges
  39. Hash table size 34679, used cells 0, node heap has 1 buffer(s)
  40. 0.00 hash searches/s, 0.00 non-hash searches/s
  41. ---
  42. LOG
  43. ---
  44. Log sequence number 0 450845
  45. Log flushed up to   0 450845
  46. Last checkpoint at  0 450845
  47. 0 pending log writes, 0 pending chkp writes
  48. 324 log i/o's done, 0.00 log i/o's/second
  49. ----------------------
  50. BUFFER POOL AND MEMORY
  51. ----------------------
  52. Total memory allocated 18682098; in additional pool allocated 912128
  53. Buffer pool size   512
  54. Free buffers       476
  55. Database pages     35
  56. Modified db pages  0
  57. Pending reads 0 
  58. Pending writes: LRU 0, flush list 0, single page 0
  59. Pages read 26, created 9, written 792
  60. 0.00 reads/s, 0.00 creates/s, 0.00 writes/s
  61. No buffer pool page gets since the last printout
  62. --------------
  63. ROW OPERATIONS
  64. --------------
  65. 0 queries inside InnoDB, 0 queries in queue
  66. Main thread id 25284096, state: waiting for server activity
  67. Number of rows inserted 49, updated 4, deleted 0, read 125
  68. 0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
  69. ----------------------------
  70. END OF INNODB MONITOR OUTPUT
  71. ============================
  72.  
Any help would be greatly appreciated.
Mar 6 '09 #1
2 43909
jarea
2
It turns out that while the table was not created, the foreign key was. When I dropped the foreign key fk_line_key_products and reran the process, all worked fine.

Whew!
Mar 6 '09 #2
Just a note for those that stumble across this. Check the collation of the 2 tables as these must also match.
Nov 10 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Morten Gulbrandsen | last post by:
USE company; DROP TABLE IF EXISTS EMPLOYEE; CREATE TABLE EMPLOYEE ( # PK SSN CHAR(9) NOT NULL, # FK SUPERSSN CHAR(9), DNO INT NOT NULL DEFAULT 1, CONSTRAINT EMPPK
0
by: CoOL! . | last post by:
Hello, I found the key to solve this problem in: http://darkstar.ist.utl.pt/mysql/doc/en/InnoDB_foreign_key_constraints.html You'll probably need an INDEX for that new foreign key you are...
0
by: NW | last post by:
Hi I had error message "Can't create table *.frm (errno:121)" when I was trying to create an innoDB table in MySQL Server 4.0.15. I know that the reason is the .frm files was deleted by some one...
2
by: blumi | last post by:
I get this error when I try to create the following tables: ERROR 1005 (HY000): Can't create table '.\haps\transcript.frm' (errno: 150) create table teaching ( ProfId int, CrsCode...
0
by: bnthorat80 | last post by:
How can i create table in runtime in window application in visual studio 2005 in vb.net on click event of button. plz help
6
by: daveftl | last post by:
Good day to all...i always have this error after making an ALTER command ... #1005 - Can't create table '.\_ftl\#sql-918_f.frm' (errno: 150) whats the cause of this error...thanks in advance..
2
luckysanj
by: luckysanj | last post by:
I have problem on creating academic table. I need to input from html form to different academic table. But how to create academic table for my requirement. I have different leve of student....
2
by: RoshiniKamath | last post by:
Errror Code:1005 Can't create table '.\test\#sql-580_1.frm' (errno: 150)..... I got this errror when i tried to create a foregin key.......Please help me out on this error.....
3
by: SteveP26 | last post by:
Hi guys, I keep getting this error message (below) when I try to run the sql query for my database Heres the code, but the error message only applies to the LAST table (SalesCopy), I have no...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.