I'm trying to add a foreign key to my table (oc_booking) but when I do the alter table query it returns "ERROR 1215 (HY000): Cannot add foreign key constraint" and I don't know why.
I tried using SQL (terminal) and HeidiSQL (a tool similar to MySQL Workbench).
This is the reference table (oc_user):
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| user_id | int(11) | NO | PRI | NULL | auto_increment |
| user_group_id | int(11) | NO | | NULL | |
| username | varchar(20) | NO | | NULL | |
| password | varchar(40) | NO | | NULL | |
| salt | varchar(9) | NO | | NULL | |
| firstname | varchar(32) | NO | | NULL | |
| lastname | varchar(32) | NO | | NULL | |
| email | varchar(96) | NO | | NULL | |
| image | varchar(255) | NO | | NULL | |
| code | varchar(40) | NO | | NULL | |
| ip | varchar(40) | NO | | NULL | |
| status | tinyint(1) | NO | | NULL | |
| date_added | datetime | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
This is the table where I want to add a foreign key constraint (oc_booking):
+----------------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-----------+------+-----+-------------------+-----------------------------+
| appointment_id | int(11) | NO | PRI | NULL | auto_increment |
| id_supplier | int(11) | NO | MUL | NULL | |
| id_user | int(11) | NO | | NULL | |
| date | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+----------------+-----------+------+-----+-------------------+-----------------------------+
And this is my query:
ALTER TABLE oc_booking ADD FOREIGN KEY (id_user) REFERENCES oc_user(`user_id`);
Related
op_dc_ip_address_alloc:
Added vlan field in this table.
desc op_dc_ip_address_alloc;
+----------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| ip_address | char(40) | NO | MUL | NULL | |
| data_center_id | bigint(20) unsigned | NO | MUL | NULL | |
| pod_id | bigint(20) unsigned | NO | MUL | NULL | |
| nic_id | bigint(20) unsigned | YES | | NULL | |
| reservation_id | char(40) | YES | | NULL | |
| taken | datetime | YES | | NULL | |
| mac_address | bigint(20) unsigned | NO | | NULL | |
| vlan | int(10) unsigned | YES | | NULL | |
+----------------+---------------------+------+-----+---------+----------------+
Host_pod_ref:
desc host_pod_ref;
+------------------+---------------------+------+-----+----------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------------------+------+-----+----------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | MUL | NULL | |
| uuid | varchar(40) | YES | UNI | NULL | |
| data_center_id | bigint(20) unsigned | NO | MUL | NULL | |
| gateway | varchar(255) | NO | | NULL | |
| cidr_address | varchar(15) | NO | | NULL | |
| cidr_size | bigint(20) unsigned | NO | | NULL | |
| description | varchar(255) | YES | | NULL | |
| allocation_state | varchar(32) | NO | MUL | Enabled | |
| external_dhcp | tinyint(4) | NO | | 0 | |
| removed | datetime | YES | MUL | NULL | |
| owner | varchar(255) | YES | | NULL | |
| created | datetime | YES | | NULL | |
| lastUpdated | datetime | YES | | NULL | |
| engine_state | varchar(32) | NO | | Disabled | |
+------------------+---------------------+------+-----+----------+----------------+
These are two tables, i am trying to insert values in op_dc_ip_address_alloc as follows:
INSERT INTO `cloud`.`op_dc_ip_address_alloc` (ip_address, data_center_id, pod_id, mac_address)
VALUES ('10.147.29.160', 1, 11, (select mac_address from `cloud`.`data_center` where id=1));
I am facing error as
Cannot add or update a child row: a foreign key constraint fails (cloud.op_dc_ip_address_alloc, CONSTRAINT fk_op_dc_ip_address_alloc__pod_id FOREIGN KEY (pod_id) REFERENCES host_pod_ref (id) ON DELETE CASCADE)
can anyone please help me on this.
INSERT INTO cloud.op_dc_ip_address_alloc (ip_address, data_center_id, pod_id, mac_address)
VALUES ('10.147.29.160', 1, 11, (select mac_address from clou``data_center where id=1));
Are you sure exist id=11 in the host_pod_ref table ?
I think this is the reason for the issue.
So I have 3 tables:
Pokemon:
+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| National_ID | int(11) | NO | PRI | NULL | |
| Picture | longblob | YES | | NULL | |
| Name | varchar(15) | NO | | NULL | |
| Generation_ID | int(11) | NO | | NULL | |
| Type1 | varchar(8) | NO | | NULL | |
| Type2 | varchar(8) | YES | | NULL | |
+---------------+-------------+------+-----+---------+-------+
Stats:
+-----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| National_ID | int(11) | NO | PRI | NULL | |
| Health_Points | int(11) | NO | | NULL | |
| Attack | int(11) | NO | | NULL | |
| Defense | int(11) | NO | | NULL | |
| Special_Attack | int(11) | NO | | NULL | |
| Special_Defense | int(11) | NO | | NULL | |
| Speed | int(11) | NO | | NULL | |
| Ability1 | varchar(20) | NO | | NULL | |
| Ability2 | varchar(20) | YES | | NULL | |
+-----------------+-------------+------+-----+---------+-------+
Misc:
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| National_ID | int(11) | NO | PRI | NULL | |
| Evolves_From | varchar(15) | YES | | NULL | |
| Species | varchar(20) | NO | | NULL | |
| Height_inch | int(11) | NO | | NULL | |
| Weight_lbs | int(11) | NO | | NULL | |
| Capture_Rate | int(11) | NO | | NULL | |
+--------------+-------------+------+-----+---------+-------+
My problem is that the National_ID in the Stats table points to the National_ID in the Pokemon table, but I cannot do the same for the Misc table.
Whenever I try to add a foreign key in Misc to point to the Pokemon table, I get a duplication error. Help please! Thank you
From the error message, it looks like you already have a foreign key constraint with name 'FK_National_ID'. Try changing it to 'FK_Pokemon_National_ID' or something and it should work.
Here is the SQL Fiddle.
Every FOREIGN KEY declaration that a table & list of column names REFERENCES a table & list of column names needs a unique name. (If you don't give one explicitly then the DBMS will make one up.)
mysql> desc students;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| AdmNo | int(5) | NO | PRI | NULL | |
| Name | varchar(40) | YES | | NULL | |
| Class | int(2) | YES | | NULL | |
| Sec | char(1) | YES | | NULL | |
| RNo | int(2) | YES | | NULL | |
| Address | varchar(35) | YES | | NULL | |
| Phone | int(9) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
7 rows in set (0.02 sec)
mysql> desc sports;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| AdmNo | int(5) | YES | | NULL | |
| Game | varchar(35) | YES | | NULL | |
| CoachName | varchar(30) | YES | | NULL | |
| Grade | char(1) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.02 sec)
I want to add Foreign Key to my second table (sports) for column AdmNo.
Try like this:
ALTER TABLE sports
ADD FOREIGN KEY (AdmNo)
REFERENCES students(AdmNo)
ALTER TABLE students
ADD FOREIGN KEY (AdmNo)
REFERENCES sports(AdmNo)
I want to do something along the lines of this:
delete c.* from Club c
where c.id in(select club_id from ClubAttrValues where value='E5');
but when I try this I get the error
Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`foroige`.`clubattrvalues`, CONSTRAINT `FKAE90CBE9EE3B7895` FOREIGN KEY (`club_id`) REFERENCES `Club` (`id`))
Is there any way to delete everything to do with the every club in Club where the value in ClubAttrValues is 'E5'. There are multiple rows in ClubAttrValues with the same club_id, which would all ideally be deleted, and club_id references id in Club.
I have tried setting ON DELETE CASCADE in MySQL workbench but even this seems to make no difference.
The Club table looks like this:
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| created | datetime | YES | | NULL | |
| deleted | tinyint(1) | NO | | NULL | |
| modified | datetime | YES | | NULL | |
| address1 | varchar(255) | NO | | NULL | |
| address2 | varchar(255) | YES | | NULL | |
| country | varchar(255) | YES | | NULL | |
| county | varchar(255) | NO | | NULL | |
| email | varchar(255) | YES | | NULL | |
| fax | varchar(255) | YES | | NULL | |
| mobile | varchar(255) | YES | | NULL | |
| name | varchar(255) | NO | | NULL | |
| postcode | varchar(255) | YES | | NULL | |
| telephone | varchar(255) | YES | | NULL | |
| town | varchar(255) | YES | | NULL | |
| type | varchar(255) | NO | | NULL | |
| parent_id | bigint(20) | YES | MUL | NULL | |
| lft | bigint(20) | NO | | NULL | |
| rgt | bigint(20) | NO | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
and my ClubAttrValues table:
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| value | text | YES | | NULL | |
| club_id | bigint(20) | YES | MUL | NULL | |
+---------+--------------+------+-----+---------+----------------+
Before deleting try:
SET FOREIGN_KEY_CHECKS=0
Set it against to 1 when you are done.
It works when you set on delete cascade for the foreign key column and then
delete c
from Club c
inner join ClubAttrValues a on a.club_id = c.id
where a.value = 'E5'
See this SQLFiddle demo
I'm trying to insert a record to QSqlRelationalTable. If setRelation is called before, foreign key constraint fails.
customers table:
+-------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(45) | YES | | NULL | |
| last | varchar(45) | YES | | NULL | |
| email | varchar(45) | YES | | NULL | |
| phone | varchar(45) | YES | | NULL | |
| fax | varchar(45) | YES | | NULL | |
| address | text | YES | | NULL | |
| customer_types_id | int(11) | NO | MUL | NULL | |
+-------------------+-------------+------+-----+---------+----------------+
customer_types table:
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(45) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
Code:
model = new QSqlRelationalTableModel(this);
model->setTable("customers");
model->setRelation(7,QSqlRelation("customer_types", "id", "name"));
// ...
QSqlRecord record = model->record();
record.setValue("name",ui->lineEditName->text());
// Other fields
record.setValue(7,QVariant("1")); // '1' exists in customer_types
qDebug() << model->insertRecord(-1,record);
qDebug() << model->lastError().text();
Output:
false
"Cannot add or update a child row: a foreign key constraint fails
(`doors`.`customers`, CONSTRAINT `fk_customers_customer_types` FOREIGN KEY
(`customer_types_id`) REFERENCES `customer_types` (`id`)
ON DELETE NO ACTION ON UPDATE NO ACTION) QMYSQL3: Unable to execute statement"