mysql foreign key, wont create mysql error message is Vague - mysql

ALTER TABLE cfg_scripts
ADD CONSTRAINT cfg_scripts_priority_fk
FOREIGN KEY (priority)
REFERENCES cfg_script_priorities(script_priority);
Lookup Error - MySQL Database Error: Cannot add foreign key constraint.
mysql fails it tell me actually why it cant
select priority
from cfg_scripts
where coalesce(priority,-1) not in (select script_priority from cfg_script_priorities)
The above query returns no rows, so no key violations.
data types the same, can't see problem
would help if mysql error message could actually tell you the problem... i find mysql's error messaging terrible....

Related

Strange MySQL foreign key error

For an unknown reason I cannot create MySQL foreign keys anymore.
Symptoms:
This is happening on all databases and tables in my MySQL installation, with no prior change from me.
When trying to tick the MySQL Workbench foreign table field box to add a foreign key the box is refusing to tick.
Receiving an error General error: 1215 Cannot add foreign key constraint when trying to programatically add a foreign key.
My SHOW ENGINE INNODB STATUS; output for foreign keys:
LATEST FOREIGN KEY ERROR
------------------------
2017-03-23 20:11:34 0x23c4 Error in foreign key constraint of table timu/#sql-17bc_1a:
foreign key (user_id) references users (id) on delete restrict:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
Things I have tried:
Made sure the database, server and tables and columns all follow the same charset and collation
Made sure everything is InnoDB
Made sure the columns are of the exact same type
Uninstalled all MySQL components except the MySQL installer and then reinstalled everything from scratch.
All this especially the fact the this is happening across all tables and databases on my local MySQL leads me to believe that it has to be something with the MySQL instance.

Unable to create/drop a foreign key on a MySQL table

I have searched for the answer to this and only come up with the basic answers on creating/dropping a foreign key etc.
I have a large table and created a foreign key, however during creation the server crashed so the key was not created.
But, if I try to create the key I get a SQL 1005 error "duplicate key on write or update", and if I try to drop the key I get a SQL 1025 error "cannot delete a parent row" which after querying the Innodb engine status translates to "cannot find a constraint with the given id". I cannot find the key by querying the information_schema, so how does MySQL know of its existence when I try to create it and yet cannot find it when I try to drop it, it must be logged somewhere. I am sort of stuck and hoping that I do not have to delete and recreate the table. Any ideas on resolving this welcome.

Magento reindex catalog product flat error: Error in foreign key constraint

I have an error when I try to reindex Magento 1.7 catalog product flat index (through admin panel or command line).
On the command line I get the following error:
Product Flat Data index process unknown error:
exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1005
Can't create table 'databasename.#sql-340a_22a3' (errno: 121)' in
/var/www/clients/client1/web16/web/lib/Zend/Db/Statement/Pdo.php:228
When I look in mysql using 'SHOW ENGINE INNODB STATUS;', I get the following error:
160830 10:17:09 Error in foreign key constraint creation for table
`databasename`.`#sql-340a_2160`.
A foreign key constraint of name
`databasename`.`FK_MAGE_MAGE_CAT_PRD_FLAT_1_ENTT_ID_MAGE_CAT_PRD_ENTT_ENTT_ID`
already exists. (Note that internally InnoDB adds 'databasename'
in front of the user-defined constraint name.)
Note that InnoDB's FOREIGN KEY system tables store
constraint names as case-insensitive, with the
MySQL standard latin1_swedish_ci collation. If you
create tables or databases whose names differ only in
the character case, then collisions in constraint
names can occur. Workaround: name your constraints
explicitly with unique names.
Any idea how to fix this?
Disable foreign_key_checks MySQL option before reindex
SET foreign_key_checks = 0;
// job
SET foreign_key_checks = 1;
I don't think that disabling foreign key checks is a good idea as it can lead to corrupted lines. In fact, this error is the result of some corrupted lines already.
You may read this thread which is, to me, a better way to resolve this issue: https://magento.stackexchange.com/questions/9974/product-flat-data-wont-reindex/354240

Laravel4 Entrust - Installation issue with foreign key constraint

I am installing the laravel 4 package Entrust.
I create the migration file.
When i try to run the migration i get an error regarding the foreign key constraint.
Is this an issue pretty regarding some PhpMyAdmin settings?
I copy under the error directly from powershell:
PS C:\wamp\www\lab\x_pat> php artisan entrust:migration
Tables: roles, assigned_roles, permissions, permission_role
An migration that creates 'roles', 'assigned_roles', 'permissions', 'permission_role' tables will be created in app/data
base/migrations directory
Proceed with the migration creation? [Yes|no]yes
Creating migration...
Migration successfully created!
PS C:\wamp\www\lab\x_pat> php artisan migrate
Migration table created successfully.
[Exception]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table assigned_roles add const
raint assigned_roles_user_id_foreign foreign key (user_id) references users (id)) (Bindings: array (
))
[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
migrate [--bench[="..."]] [--database[="..."]] [--path[="..."]] [--package[="..."]] [--pretend] [--seed]
Foreign key constraints are not enforced by phpMyAdmin (which is a web interface) but by the MySQL server.
There are many possibilities for this error; it might be because the tables' structure do not permit it (missing indexes for example), or because of the existing data itself that does not respect the constraint you want to add.
The error is due to the missing reference on adding the foreign key (user_id) references users (id). Please ensure that the user table is exist prior to running the entrust migration.
I had to ensure my users table had its 'id' column attribute set to 'unsigned'. This matches the user_id column attribute in the entrust assigned_roles table. That allowed the migration to proceed for me.
SOLVED:
In PhpMyAdmin in had just to go in the table that contains the foreign key and press on INDEX button. In this way an index is created on that foreign key and then in RELATION VIEW is possible to assign the foreign key to the proper id of the proper table.
I think was just that the solution.
The root problem is in the file EntrustSetupTables in the migration folder, in this lines :
$table->foreign('user_id')->references('id')->on('')
->onUpdate('cascade')->onDelete('cascade');
Just put the name of the table users after the .on() and the problem will be solved.

Mysql cannot add or update child row, Foreign key related failure

Error: cannot add or update a child row: foreign key constraint fails
Following is the code that is creating the error:
ALTER TABLE 'catalog_eav_attribute'
ADD CONSTRAINT 'FK_CATALOG_EAV_ATTRIBUTE_ID'
FOREIGN KEY ('attribute_id') REFERENCES 'eav_attribute' ('attribute_id)'
ON DELETE CASCADE ON UPDATE CASCADE;
I uploaded the Structure of SQL and its has no issue , but when i insert the data i am getting the above related error. I read somewhere and i predict its because of lousy data.What are the other possibilities for the error ? Any suggestions or solution will be great.
"For storage engines other than InnoDB, MySQL Server parses the FOREIGN KEY syntax in CREATE TABLE statements, but does not use or store it."
From: http://dev.mysql.com/doc/refman/5.5/en/ansi-diff-foreign-keys.html
I'm not sure, but if your table is MyISAM and not InnoDB, your syntax might not work.
The error message means whatever number you're trying to put into the attribute_id column doesn't yet exist in the eav_attribute.attribute_id column.
Can you insert a value that does already exist in eav_attribute.attribute_id? (You probably can.)
Can you provoke the same error by trying to insert a value that doesn't exist in eav_attribute.attribute_id?
Reconcile the differences, and you're done. You need to determine which attribute ids don't exist in eav_attribute.attribute_id, and fix that.