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"
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.
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`);
I need help debugging a migration.
I tried to execute each query step by step to found where django migration stops.
This query fails:
mysql> ALTER TABLE `elezioniAgendaCore_comments` ADD CONSTRAINT `elezioniAgendaCore_comments_uid_id_670175fa07fa7b47_fk_user_uid` FOREIGN KEY (`uid_id`) REFERENCES `user` (`uid`);
With Error:
ERROR 1005 (HY000): Can't create table 'activedoc.#sql-3f7_2b' (errno: 150)
I have tow tables.
TABLE user:
mysql> show columns from user;
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| uid | bigint(20) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | YES | MUL | NULL | |
| surname | varchar(50) | YES | MUL | NULL | |
| email | varchar(100) | YES | MUL | NULL | |
+-------------------+--------------+------+-----+---------+----------------+
TABLE elezioniAgendaComments:
mysql> show columns from elezioniAgendaCore_comments;
+------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| signature | varchar(255) | NO | | NULL | |
| message | longtext | NO | | NULL | |
| created | datetime | NO | | NULL | |
| published | tinyint(1) | NO | | NULL | |
| adempimento_id | int(11) | NO | MUL | NULL | |
| parentComment_id | int(11) | YES | MUL | NULL | |
| uid_id | bigint(20) | NO | | NULL | |
+------------------+--------------+------+-----+---------+----------------+
I found one table was created with MyISAM engine:
SHOW CREATE TABLE user;
[...]
) ENGINE=MyISAM AUTO_INCREMENT=307 DEFAULT CHARSET=latin1 |
The other table:
mysql> SHOW CREATE TABLE elezioniAgendaCore_comments;
[...]
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
Then I migrated to InnoDB both tables. No errors this time.
Initially the Primary Key was
(caseId,osName)
+--------------------+---------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+---------------+------+-----+-------------------+-------+
| createdBy | varchar(200) | NO | | NULL | |
| createdOn | timestamp | NO | | CURRENT_TIMESTAMP | |
| osName | varchar(200) | NO | | NULL | |
| caseId | varchar(100) | NO | MUL | NULL | |
| issueType | varchar(100) | NO | | NULL | |
| observationState | varchar(60) | YES | | NULL | |
| problemSolving | int(11) | NO | | NULL | |
| structure | int(11) | NO | | NULL | |
| logicalTransition | int(11) | NO | | NULL | |
| ownership | int(11) | NO | | NULL | |
| expectationSetting | int(11) | NO | | NULL | |
| empathy | int(11) | NO | | NULL | |
| valueAdd | int(11) | NO | | NULL | |
| comments | varchar(2000) | NO | | NULL | |
| version | int(11) | YES | | 0 | |
| showcase_escalate | varchar(30) | YES | | NULL | |
| ringback_state | varchar(200) | YES | | NULL | |
| trt_state | varchar(200) | YES | | NULL | |
| srp_state | varchar(200) | YES | | NULL | |
| id | int(11) | NO | PRI | NULL | |
+--------------------+---------------+------+-----+-------------------+-------+
In the above schema, id was AUTO INCREMENT. I removed that so I could make caseId as PRIMARY KEY. But since there are multiple records with same caseId I am not able to make the change.
At the end I just want caseId as my Primary Key.
Initially the Primary Key was (caseId,osName).
As defined in the MySQL Glossary:
primary key
A set of columns -- and by implication, the index based on this set of columns -- that can uniquely identify every row in a table. As such, it must be a unique index that does not contain any NULL values.
InnoDB requires that every table has such an index (also called the clustered index or cluster index), and organizes the table storage based on the column values of the primary key.
When choosing primary key values, consider using arbitrary values (a synthetic key) rather than relying on values derived from some other source (a natural key).
See Also clustered index, index, natural key, synthetic key.
Since you have multiple records with the same value for caseId, caseId alone cannot "uniquely identify every row" in your table; and therefore caseId alone cannot be a PRIMARY KEY.
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