So I currently have a table that has the following constraints according to SHOW CREATE TABLE:
CONSTRAINT `FK_BA62400997790DEE` FOREIGN KEY (`some_id`) REFERENCES `Other_Table` (`id`),
CONSTRAINT `FK_BA624009C8554709` FOREIGN KEY (`someOther_id`) REFERENCES `Yet_Another_Table` (`id`)
However, when using mysqldbcopy to copy the db over, I get the following error:
ALTER TABLE old_database.OriginalTable add CONSTRAINT `FK_BA62400997790DEE`
FOREIGN KEY (`some_id`,`someOther_id`)
REFERENCES `old_database`.`Other_Table`
(`id`,`id`)
ON UPDATE RESTRICT
ON DELETE RESTRICT
. Error: Query failed. 1005 (HY000): Can't create table 'old_database.#sql-602_4b5' (errno: 150)
Something is clearly going wrong here, but not sure what it is. Clearly it's somehow trying to combine the keys?
I am running version 1.3.5.
The 1.3.6 release fixed the following bug which may be related
BUG#17474810: constraint error copying the employees with mysqldbcopy
Try upgrading to something higher, currently up to version 1.6 - https://launchpad.net/mysql-utilities
I am using MySQL Utilities mysqldbcopy version 1.6.5, it still shows me something like this:
ERROR: Unable to execute constraint query
ALTER TABLE node5.books add CONSTRAINT `books_ibfk_1`
FOREIGN KEY (`publisher_id`)
REFERENCES `node5`.`publishers`
(`id`)
ON UPDATE RESTRICT
ON DELETE RESTRICT
. Error: Query failed. 1215 (HY000): Cannot add foreign key constraint
And then I found setting storage engine to InnoDB can solve this issue.
https://github.com/maghead/maghead/commit/b406abf277b525f665966f8e8ec421a229bb13c7
Related
I am trying to set a foreign key to constraint for on delete and on update to "NO ACTION", but for some reason after I apply the changes MySQL workbench changes it back to "RESTRICTED" on its own, I don't know why it's doing that.
This is the code MySQL Workbench generates when I try to change the constraints for on update and on delete to "NO ACTION"
ALTER TABLE `forums`.`post_replies`
DROP FOREIGN KEY `fk_post_replies_users`;
ALTER TABLE `forums`.`post_replies`
ADD CONSTRAINT `fk_post_replies_users`
FOREIGN KEY (`user_id`)
REFERENCES `forums`.`users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
NO ACTION and RESTRICT are synonyms.
"13.1.20.5 FOREIGN KEY Constraints":
NO ACTION: A keyword from standard SQL. In MySQL, equivalent to RESTRICT.
Workbench seems to just pick to display RESTRICT over NO ACTION (well, it has to chose one and chooses the MySQL specific one...). But it doesn't mean anything different.
I get the following error
Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at com.liferay.portal.dao.orm.hibernate.SessionImpl.flush(SessionImpl.java:144)
... 192 more
Caused by: java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails (`audit_compliance`.`audit_compliance_auditdetailes`, CONSTRAINT `auditComplianceCompanyId` FOREIGN KEY (`auditComplianceCompanyId`) REFERENCES `audit_compliance_auditcompliancecompany` (`audit)
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2054)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1467)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1135)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
I am using liferay 6.2. It has no mapping key in service builder, so I manually gave the reference in my database table.
The referencing is correct, I dont understand the reason it is giving error.
ALTER TABLE `audit_compliance`.`audit_compliance_auditdetailes`
DROP FOREIGN KEY `documentCollectionIds`;
ALTER TABLE `audit_compliance`.`audit_compliance_auditdetailes`
ADD CONSTRAINT `documentCollectionIds`
FOREIGN KEY (`documentCollectionIds`)
REFERENCES `audit_compliance`.`audit_compliance_documentcollection` (`name`);
As ServiceBuilder doesn't include foreign key references, it might use the wrong order to insert data into tables: Now you enforce the foreign key relationship, but ServiceBuilder still doesn't know about it.
Decide if you use ServiceBuilder's persistence (then you might want to go without foreign keys) or roll your own. You can still use ServiceBuilder for the service layer and just use your own persistence in the background.
I am trying to add foreign key to my table with this script:
ALTER TABLE PRM_CTY
ADD CONSTRAINT fk_PRM_CTY_PRNT_CTY
FOREIGN KEY (PRNT_CTY_ID)
REFERENCES PRM_CTY(ID);
this code work but foreign key doesnt creat. instead of this a new index is created named fk_PRM_CTY_PRNT_CTY.
my foreign key returns an index
help me why it happens?
MySQL automatically creates an index to support each foreign key constraint, unless a suitable one already exists. The creation of an index suggests -- but does not prove -- that the constraint was successfully created. It is not a sign that constraint creation failed. To verify that the constraint exists, try to insert a row that violates it.
Edited to add:
Note, too, that you should be using the InnoDB storage engine if you want enforcement of foreign key constraints. The default default was MyISAM prior to MySQL 5.5, and that engine is still available, so if you were not aware of this issue then that could be what you are using.
There are several other questions about this topic that I have gone through, but I can't seem to figure out how their solutions apply to my tables. Check out the sqlfiddle. You can see it builds the schema just fine.
Basically, one table is a table of contacts/people. The second table is a table of countries. I am attempting to create a foreign key reference between contacts.country_id and countries.id.
Now, add the following to the panel on the left side:
ALTER TABLE `ultra_contacts`
ADD INDEX `fk_test` (`country_id`),
ADD CONSTRAINT `fk_test` FOREIGN KEY (`country_id`) REFERENCES `ultra_countries` (`id`) ON UPDATE CASCADE ON DELETE CASCADE`
The alter table code is not working for some reason. Any help would be appreciated.
The error is: Schema Creation Failed: Can't create table 'db_e342e.#sql-7711_1a4d2' (errno: 150): Using a 3rd party program (HeidiSQL) the error is a bit more detailed:
Foreign key constraint is incorrectly formed
You're trying to use foreign keys on a MyISAM table, which is not allowed (they only work with InnoDB). Take a look here: http://sqlfiddle.com/#!2/64951 All I've changed from your original is the table type (from MyISAM to InnoDB) and then I added the constraint. Worked fine.
Full disclosure - I'm the author of SQL Fiddle :)
I am giving following error while adding the object into database.
Foreign key 'fk4150' references object 'TESTPOLY2' which is not a user
table. Could not create constraint. See previous errors. in SQL:
ALTER TABLE [WORKLOG2SETUP] ADD CONSTRAINT fk4150 FOREIGN KEY
(related_id) REFERENCES TESTPOLY2
Please let me know if anyone knows solution.
Cheers,
Pravin