I have slight problems with Laravel Migration. I have one table that have parent row itself, and when I try to install passport, passport is rollingback migrations and with one of them is making problems like:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or
update a parent row: a foreign key constraint fails (SQL: drop table
if exists e_categories)
Table looks like:
Schema::create('e_categories', function (Blueprint $table) {
$table->uuid('id');
$table->primary('id');
$table->string('name');
$table->uuid('parent_id')->nullable();
$table->foreign('parent_id')->references('id')->on('e_categories')->onDelete('cascade');
$table->timestamps();
});
And down
Schema::dropIfExists('e_categories');
BTW in this migration are stored other migrations for categories, but this one is making a problem.
I think you should drop the Foreign key first
in down:
$table->dropForeign(['parent_id']);
Schema::dropIfExists('e_categories');
Related
Schema::create('a', function(Blueprint $table) {
$table->bigInteger('id');
$table->primary('id');
});
Schema::create('b', function(Blueprint $table) {
$table->bigInteger('id');
$table->bigInteger('a_id');
$table->foreign('a_id')->references('id')->on('a');
});
Schema::create('c', function(Blueprint $table) {
$table->bigInteger('id');
$table->bigInteger('a_id');
$table->foreign('a_id')->references('id')->on('a');
});
Running this with php artisan migrate on this gives me a
ORA-02264: name already used by an existing constraint.
It seems that migrate creates a constraint on b called A_ID_PK, then it tries to create a constrain on c called A_ID_PK and errors since there are 2 A_ID_PK constraints on the A.id. Am I correct, and if so is there a solution?
From the error utility, ORA-02264 error means ORA-02264: Name already used by an existing constraint.
And the definition is that specified constraint name has to be unique. So the solution is specify a unique constraint name for the constraint.
Problem and workaround
Not sure but the problem is that while the table may have not been created, the constraint_name might be exists in the dba_constraints view. You can check this like:
select
table_name
from
dba_constraints
where constraint_name = upper ('A_ID_PK');
If you do not have already used this constraint_name for other table.
Your solution is either of the below:
Use another constraint name, that unique to the table.
Make sure that this constraint name exists for a table.
I'm trying to move my local laravel database up to RDS, I have had lots of problems with Elastic beanstalk so I decided to go a different way. When I migrate I get the following error with every foreign key I have:
[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error:
1215 Cannot add foreign key constraint (SQL: alter table videos add
constraint videos_object_id_foreign foreign key (object_id)
references objects (id) on delete cascade)
PDOException
[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
I have looked into RDS's foreign key constraints and they say they are not enforced, but it says they can be used for informational purposes.
Every foreign key is unsigned.
Schema::create('videos', function (Blueprint $table) {
$table->increments('id');
$table->integer('object_id')->unsigned()->nullable();
$table->foreign('object_id')->references('id')->on('objects')->onDelete('cascade');
$table->integer('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->rememberToken();
$table->timestamps();
});
Note: I have also tried separated the forein keys into a separate migration after the table was created and had no luck.
To find the specific error run this:
SHOW ENGINE INNODB STATUS
And look in the LATEST FOREIGN KEY ERROR section.
The data type for the child column must match the parent column exactly.
Also, you should run the query set foreign_key_checks=0 before running the DDL so you can create the tables in an arbitrary order rather than needing to create all parent tables before the relevant child tables.
I try to add a foreign key constraint in migrations. For some reason it does work when I set it to nullable, but it doesn't work when I make it not nullable. Why does this happen and how can I solve this?
This does work:
Schema::create('role_user', function (Blueprint $table){
$table->increments('id');
$table->integer('role_id')->unsigned()->index()->nullable();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('set null');
$table->integer('user_id')->unsigned()->index()->nullable();
$table->foreign('user_id')->references('id')->on('users')->onDelete('set null');
$table->timestamps();
});
This throws the exception:
Schema::create('role_user', function (Blueprint $table){
$table->increments('id');
$table->integer('role_id')->unsigned()->index();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('set null');
$table->integer('user_id')->unsigned()->index()->nullable();
$table->foreign('user_id')->references('id')->on('users')->onDelete('set null');
$table->timestamps();
});
Both the role and user tables already excist before making these constraints. I want them to be not nullable (so they must be filled). The error:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table role_user add constraint role_user_role_id_foreign foreign key (role_id) references roles (id) on delete set null)
The migrations happen in the order that you specify in the filename. Maybe your roles table hasn't been created by the time this migration is run and your DB complains about trying to reference a field in a (yet) non-existent table?
For example: 2016_03_27_0000_first will be executed before 2016_03_28_0000_second
UPDATE: Added solution.
I noticed that you have "onDelete('set null')" for the field that you are trying to set as non-nullable. That would also cause a problem if the role was deleted, since it would then try to set the value to null.
You should set both foreign keys to:
->onDelete('cascade')
This way if the user or the role get deleted, any associations related to them would also get deleted automatically and not simply set to null or left hanging around.
Cheers
I've multiple packages and users can only have one package at a time, not multiple. So, I've created one to one relationship and added a foreign key in my users table.
Here's the relevant users table:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->string('role');
$table->unsignedInteger('package_id');
$table->rememberToken();
$table->nullableTimestamps();
$table->foreign('package_id')
->references('id')
->on('packages');
});
}
And here's the packages table:
public function up()
{
Schema::create('packages', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('price');
$table->integer('space');
$table->string('space_type');
$table->string('trial')->nullable();
$table->string('trial_period')->nullable();
$table->string('password_protected_links')->nullable();
$table->nullableTimestamps();
});
}
However, when I ran php artisan migrate:refresh than it appeared the following error:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table 'clouder.#sql-834_1
4' (errno: 150) (SQL: alter table `users` add constraint users_package_id_f
oreign foreign key (`package_id`) references `packages` (`id`))
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table 'clouder.#sql-834_1
4' (errno: 150)
However, I believe I've created the foreign key correctly. Is there anything else that I've made a mistake?
I've fixed the issue by myself now. The issue I was experiencing is caused by the order of the migration. The create_users_table migration comes with Laravel 5.2 out of the box as such when I've created new migrations create_packages_table the order of the create_users_table migration on the first and it was loading before the create_packages_table. As such when the query trying to create the foreign key it can't find the column on the packages table. So, simply renaming the create_users_table date to ensure it loads after the create_packages_table fixed the issue.
I hope someone may find it helpful in some days.
I have created two migrations with the Jeffrey Way generators for Laravel 5. Namely: php artisan make:migrate:schema create_roles_table --schema='name:string, description:text' and php artisan make:migrate:pivot user role.
Now, my MySQL database is defaulted to InnoDB, which I've read should be. I also moved the foreign key setup to Schema::table() instead of Schema::create('role_user'). There shouldn't be anything wrong with my migrations, yet they error out with: General error: 1215 Cannot add foreign key constraint (SQL: alter table role_user add constraint role_user_user_id_foreign foreign key (user_id) references user (id) on delete cascade).
Schema::create('role_user', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->integer('role_id')->unsigned()->index();
$table->integer('user_id')->unsigned()->index();
$table->primary(['role_id', 'user_id']);
});
Schema::table('role_user', function(Blueprint $table){
$table->foreign('user_id')->references('id')->on('user')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('role')->onDelete('cascade');
});
I have called $table->engine = 'InnoDB'; in every migration. Also have I set all id columns to unsigned, even though they were like that already. I am clueless, who helps me out..
Check to see if you have already migrated the tables users and roles. Otherwise you will not be able to create a foreign key. Check your migration order as well. If you are migrating this one before the users and roles it will fail.
Well, apparently this was a huge typo. user instead of users, for example. I had hoped Laravel would get that right, but unfortunately Laravel and I as well didn't.
This might be related to your SQL engine, check your engine (mariadb, mysql, ...)
And then read the documentation from your engine regarding foreign keys.
Quick solution
Remove the onDelete('cascade') and run the migration.
You can add it manually from phpMyAdmin later on