strange thing about foreign key in laravel - php

I meet Strange Problem when I use foreignkey in laravel
When I used unsignedBigInteger Foreign key work
Schema::table('posts', function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
});
But if I just edit Datatype it to
$table->*unsignedInteger('user_id');
** or
$table->Integer('user_id');
Foreign key Not work
I don't know why this happend

if you want to make your id datatype unsignedInteger or Integer you need first to change the users id datatypes in its migration file to the same data type because of a foreign key must have the same data type between the two columns

Related

how can I use the same foreign key twice in a single table?

I came across a problem. So I have this migration in Laravel:
{
Schema::create('transfers', function (Blueprint $table) {
$table->id();
$table->foreignId('from_inventory_id')->constrained();
$table->foreignId('to_inventory_id')->constrained();
});
}
What I am trying to do is to transfer products from an inventory to another. The problem is, if I name them the way I did above, Laravel won't recognize them as foreign keys because they have to be 'inventory_id', but I also can't have two columns named the same.
How can I name them whatever I want and apply to them the same foreign key?
You should use the old syntax:
$table->unsignedBigInteger('from_inventory_id');
$table->foreign('from_inventory_id')->references('id')->on('inventories');
$table->unsignedBigInteger('to_inventory_id');
$table->foreign('to_inventory_id')->references('id')->on('inventories');

Foreign key constraint is incorrectly formed - Laravel

I'm on Laravel 5.8 on a XAMPP stack.
Consider these two migrations to create two tables:
post_categories table.
Schema::create('post_categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('slug');
$table->string('status');
$table->timestamps();
});
posts table.
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('slug');
$table->mediumText('description');
$table->integer('views');
$table->integer('post_category_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->timestamps();
$table->foreign('post_category_id')->references('id')->on('post_categories');
});
When I run php artisan migrate, I'm getting the following error:
General error: 1005 Can't create table testdb.#sql-38d8_102 (errno: 150 "Foreign key constraint is incorrectly formed").
I've tried running both:
php artisan migrate
php artisan migrate:fresh
I also tried changing the post_category_id field to a regular integer, instead of unsigned: no difference.
Also restarted the MySQL server and Apache service, didn't make a difference.
The post_categories migration runs before the posts migration. The posts table gets created, but the foreign key doesn't.
Why is this error being thrown and how do I solve it?
This might be happening because you're trying to create a foreign key with an integer field to a big integer field. In your posts migration, instead of this
$table->integer('post_category_id')->unsigned();
do this:
$table->bigInteger('post_category_id')->unsigned();
Instead of this:
$table->integer('post_category_id')->unsigned();
Try this:
$table->unsignedBigInteger('post_category_id');
I had the same error where I wanted to link two tables, users tables and deposits tables. I tried many options but they didn't work. However, this is what worked for me on create deposits migration:
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

Laravel Migrate creating duplicate foreign key names

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.

Laravel - 1215 Cannot add foreign key constraint

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

Laravel 4: Migration - Schema::table - Text column automatically creates primary unique index

I am creating a migration in Laravel with the following function:
public function up()
{
Schema::create('translate_item', function(Blueprint $table)
{
$table->increments('id');
$table->integer('lesson_id');
$table->text('lang_1');
$table->text('lang_2');
$table->timestamps();
});
}
The above creates the text fields as primary unique index as shown below:
Can anyone tell why this is happenning and how to create text fields without making it primary index?
In fact they aren't created as primary key nor with unique index.
It maybe look like it. The fields are greyed out, because that's how phpMyAdmin displays columns you can't make primary key or unique.
No need to worry. text() does nothing you wouldn't expect.

Categories