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.
Related
New to MySQL / PHPMyAdmin although I've done some SQL before. I've tried to create a foreign key constraint on a table and was successful in creating this constraint with one table (Account - Standard) but not for (Account - Premium). This link contains screenshots and further explanations of the errors I am receiving. https://imgur.com/a/0s9VUA5
Constraint name should be unique. For example:
fk_username_standard, fk_username_premium ...
I need to create a foreign key, but executing the following results in the error: "Error Code: 1826. Duplicate foreign key constraint name 'FK_ProjectBase_Program'"
alter table ipos5.ProjectBase
add constraint FK_ProjectBase_Program foreign key (Program) references Program(OID);
If I execute:
select *
from information_schema.TABLE_CONSTRAINTS
where CONSTRAINT_TYPE = 'FOREIGN KEY'
result = def ipos5 FK_ProjectBase_Program ipos5 projectbase FOREIGN KEY
I can see the existing key definition, but if I show the structure for the target TABLE_NAME, it is not there.
This is on an active database with a large amount of data, using InnoDB, so dump/restore is a very last resort.
I am using a 3rd party framework, which does not allow me to manually specify a foreign key name (so I HAVE to use the one specified), but my application errors during startup because it cannot create the key.
Is there a way to rebuild the information_schema database? I am really hoping to avoid doing a dump and rebuild of the application database, as it is quite large.
I ended up duplicating the table structure, copying the data into it, dropping the original table, then re-creating it and copying the data back. Orphaned foreign key reference is now gone.
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.
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.
I have a table User that has many child tables defined in User class under the static hasMany
grails.
I have no problem when doing User.get(3).delete() in grails. It automatically delete that user and all its child table rows.
But when I want to perform the same operation in MySQL workbench. I get Error thrown by MySQL:
ERROR 1451: Cannot delete or update a parent row: a foreign key constraint fails (`test_db`.`search_stat`, CONSTRAINT `FK7A3CFFB6E64DB41` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`))
SQL Statement:
DELETE FROM `test_db`.`user` WHERE `id`='3'
I dont know what is the problem here with MySQL.
Where did you read about the "ON DELETE CASCADE" in the documentation? I haven't found it here. The poster of this post had to manually add this as well to get the desired behaviour.
If grails is really supposed to add this, have you tried deleting the DB and having it re-created by grails (at least in development environment)? Maybe the current schema was generated before you added the belongsTo or something?
Also check out this Blog post about GORM Gotchas regarding hasMany and belongsTo.