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
- Get comma separated ids from table using MySQL
- How to retrieve data from two tables with one SQL statement
- Clear the terminal screen in MySQL within the Windows powershell
- How to get yesterday's date in MySQL ?
- Change existing MySQL table column id to autoincrement
- SQL query to delete all rows older than 30 days
- Create column after a column in existing table using query in MySQL
- MySQL create procedure example
- How to drop function in MySQL ?
- Self join in mysql employee manager example
- Create Index in MySQL with example
- How to get a list of MySQL views?
- Database connection using mySQLi with object-oriented
- How to check the MySQL version
- Check constraint in MySQL
- Insert multiple rows in a single MySQL query
- Cannot drop index 'fk_role_id': needed in a foreign key constraint
- How to get the list of all constraints in MySQL database
- Get data in random order with limit from table using mysql
- How to check column exists or not in table using MySQL
- SQLSTATE[01000]: Warning: 1265 Data truncated for column 'visibility' at row 1
- Get number of connections being used by a specific user in MySQL
- Get create table Query from existing table in phpMyAdmin
- How to Rename column name in MySql ?
- SQL query to delete records older than 6 months