ERROR 3780 (HY000): Referencing column 'order_id' and referenced column 'id' in foreign key constraint 'order_items_ibfk_1' are incompatible.
ERROR 3780 (HY000): Referencing column 'order_id' and referenced column 'id' in foreign key constraint 'order_items_ibfk_1' are incompatible
. Why i am getting above error while executing the below query.
create table order_items (
id int auto_increment primary key,
order_id int,
product_id int,
quantity int,
FOREIGN KEY (order_id) REFERENCES orders(id),
FOREIGN KEY (product_id) references products(id)
);
Please provide me solution.
Here are table structure and other details related to tabels.
products table structure -
CREATE TABLE `products` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT NULL,
`price` float DEFAULT NULL,
PRIMARY KEY (`id`)
)
orders table structure -
CREATE TABLE `orders` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`total_amount` decimal(10,2) NOT NULL,
`status` enum('pending','processing') DEFAULT 'pending',
PRIMARY KEY (`id`)
)
-
Columns must have the same data type and length while defining foreign key in MySQL
The error message you're encountering, "
Referencing column 'order_id' and referenced column 'id' in foreign key constraint 'order_items_ibfk_1' are incompatible,
" typically occurs when the data types of the columns in the foreign key constraint don't match exactly.You have unsigned primary key in products and orders table so while defining the foreign key you have to assign the unsigned to foreign key column (
order_id
,product_id
).Table structure for order_items table should be look like below -
create table order_items ( id int auto_increment primary key, order_id int unsigned , product_id int unsigned, quantity int, FOREIGN KEY (order_id) REFERENCES orders(id), FOREIGN KEY (product_id) references products(id) );
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Sql
- Cannot drop index 'fk_role_id': needed in a foreign key constraint
- Create column after a column in existing table using query in MySQL
- How to get the list of all constraints in MySQL database
- Clear the terminal screen in MySQL within the Windows powershell
- MySQL create procedure example
- 1553 Cannot drop index 'posts_user_id_foreign': needed in a foreign key constraint
- How to create view in MySQL database
- How to get a list of MySQL views?
- How to check the MySQL version
- Get comma separated ids from table using MySQL
- How to check column exists or not in table using MySQL
- Difference between SQL and NoSQL databases
- Drop foreign id column (user_id) in MySQL
- Get full name of a person in SQL using function
- Create Employee table in MySQL
- How to create function in MySQL ?
- Get number of connections being used by a specific user in MySQL
- Insert multiple rows in a single MySQL query
- How to retrieve data from two tables with one SQL statement
- SQL query to delete records older than 6 months
- How to drop function in MySQL ?
- Create Index in MySQL with example
- Self join in mysql employee manager example
- SQL query to delete all rows older than 30 days
- SQLSTATE[01000]: Warning: 1265 Data truncated for column 'visibility' at row 1