I am creating 2 tables, customers and orders. The goal is to create a constraint in the orders customers column to only allow insert if customer exists.
Here is the code I am trying
CREATE TABLE other.customers (
customers_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(45) NULL,
PRIMARY KEY (customers_id)
);
Next I am creating the orders table
CREATE TABLE other.orders (
orders_id INT NOT NULL,
customer INT NOT NULL,
PRIMARY KEY (orders_id),
INDEX customers_idx (customer ASC),
CONSTRAINT customers FOREIGN KEY (customer)
REFERENCES other.customers (customers_id)
ON DELETE CASCADE ON UPDATE CASCADE
);
Both times I get no errors.But his apparently doesn't work and I cannot see the constraint. So I tried to alter the orders table
ALTER TABLE other.orders ENGINE = InnoDB ;
ALTER TABLE other.orders ADD CONSTRAINT customer
FOREIGN KEY (customer) REFERENCES other.customers (customers_id)
ON DELETE CASCADE ON UPDATE CASCADE;
But this time I get an SQL error:
Operation failed: There was an error while applying the SQL script to the database. ERROR 1215: Cannot add foreign key constraint
I am probably missing something trivial, but just can't put my finger on it.