Laravel 9 Migration ForeginKey not created - laravel

I have some problem with the Laravel 9 migrations. After i run the "php artisan migrate" command every table created with the indexes. So everthings just fine except the foregin key. I don't have any idea why, maybe someone know what causes the problem or have a solotion. Thank you all!
10.4.22-MariaDB
InnoDB
Apache/2.4.52 (Win64) OpenSSL/1.1.1m PHP/8.1.2
phpMyAdmin: 5.11.1
My migrations:
Schema::create('supplier_orders', function (Blueprint $table) {
$table->increments('Id');
$table->unsignedBigInteger('Warehouse', false)->nullable(false);
$table->unsignedBigInteger('Supplier', false)->nullable(false);
$table->dateTime('StartedAt')->nullable(false)->useCurrent();
$table->dateTime('CompletedAt')->default(null);
$table->string('PrimeVoucherNumber', 100)->nullable(false);
$table->index(['CompletedAt', 'Warehouse', 'Supplier'], 'CompletedWarehouseSupplier');
});
Schema::create('supplier_order_details', function (Blueprint $table) {
$table->increments('Id');
$table->unsignedInteger('SupplierOrder', false)->nullable(false);
$table->unsignedBigInteger('Employee', false)->nullable(false);
$table->unsignedBigInteger('Product', false)->nullable(false);
$table->unsignedDecimal('Quantity', 18, 4)->nullable(false);
$table->index(['SupplierOrder', 'Employee', 'Product'], 'OrderEmployeeProduct')->unique();
$table->index(['Product', 'SupplierOrder', 'Quantity'], 'ProductOrder');
});
Schema::table('supplier_order_details', function (Blueprint $table) {
$table->foreign('SupplierOrder', 'FK_SupplierOrderDetail_SupplierOrder')->references('Id')->on('supplier_orders')->onDelete('CASCADE')->onUpdate('NO ACTION');
});

I found it, there is no error in the migartions, just me.
I am just a big donkey.
I found it in the phpmyadmin under "Table->Structure->Realation view"
I thought that i will found it under "Table->Structure->Table structure->Indexes"
Its make no sense to me why is it there, but finally i found it.
Thanks everyone.

Related

Undefined table: 7 ERROR: relation "users" does not exist

Friends I have the following problem with laravel migrations using postgres, and when I make changes to a migration, in this case the users table, but I get an error trying to remove an index from a key, can you help me please with this problem.
This is my migration code:
public function up() {
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->integer('idProfile');
$table->string('name');
$table->string('surname');
$table->string('email')->unique();
$table->string('photo')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
public function down() {
Schema::dropIfExists('users');
Schema::table('users', function (Blueprint $table){
$table->dropPrimary('id');
$table->dropIndex('users_pkey');
});
}
response from my console:
These are the indices that list me:
This is the structure of the final table:
Comments, things to improve I am all ears
When you are running migrate:refresh, you are running the down method. In your down method, you are dropping the table, and then trying to make edits to it, which is why you are getting "users" doesn't exist.
If this is just in a development env, make your changes in the up method, and remove everything apart from dropIFExists() from the down method.
It is highly recommended that don't change the migration file...
If you need to add a new row to your table (Consider you have mytable and you want to add myrow to the table), you can write in terminal :
php artisan make:migration add_myrow_to_mytable_table
after that , edit new added migration file!
remember to add the following code in down function :
Schema::table('mytable', function (Blueprint $table) {
$table->dropColumn('myrow');
});
after all, run :
php artisan migrate
If you want to remove a column from your table just follow this one :
Laravel Migrations - Dropping columns

The value of id is always "0" Laravel on cpanel

Good Day
I have a Laravel 7 project that's having some issues, first i have this error
The solution that i found for that error is to edit 'strict' value to false in config/database.php. This fixed the error. But now i just realized that every time I create a user the value of the id is always "0" example
Here is my migration
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('phone_number');
$table->string('client_address');
$table->rememberToken();
$table->timestamps();
});
}
it is because of your id field in table is not AuthoIncrement
first of all remove users table in database
then remove create_users_table record in migrations table in database
and then change your migration to this
$table->bigIncrements('id');
and then php artisan migrate

Cannot delete or update a parent row: a foreign key constraint fails even there is onDelete Cascade and onUpdate cascade?

Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails
users Table
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
cv table
Schema::create('cvs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id')->nullable();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->string('name')->nullable();
$table->string('contact')->nullable();
$table->string('contact2')->nullable();
$table->string('gender')->nullable();
$table->string('email')->unique()->nullable();
$table->string('paddress')->nullable();
});
Reason can be:
your migration of CSV is before Users.
Take a look migration names (and dates). It should be:
2019_02_01_101010_create_users_table.php
2019_02_02_101010_create_csv_table.php (look, date is 2019_02_02)
Then fire php artisan:migrate
Be sure that in other migrations you have proper dates. Migration can not work if one table want relation with table that was not created yet.
Good luck!
In this situation you need to migrate the users table first, then the cvs
to do that:
php artisan migrate --path=/database/migrations/selected
if you have run migration before you need to migrate
php artisan migrate --path=/database/migrations/2014_10_12_000000_create_users_table.php
then run
php artisan migrate
It'll automatically migrate all the rest of the tables for you
Hope this helps

Laravel 5.6 set migration nullable foreign id

This error popup:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`ltfrbr10infosystem`.`franchises`, CONSTRAINT `franchises_operator_id_foreign` FOREIGN KEY (`operator_id`) REFERENCES `operators` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
Laravel Migrration:
public function up()
{
Schema::create('franchises', function (Blueprint $table) {
$table->increments('id');
$table->integer('operator_id')->nullable()->unsigned();
$table->foreign('operator_id')->references('id')->on('operators')->onDelete('cascade')->onUpdate('cascade');
$table->string('case_number')->nullable();
$table->string('business_address')->nullable();
$table->date('date_granted')->nullable();
$table->date('expiry_date')->nullable();
$table->string('route_name')->nullable();
$table->string('deno')->nullable();
$table->integer('authorize_units')->nullable();
$table->string('remarks')->nullable();
$table->timestamps();
});
}
I have tried this but still it gives me error
$table->integer('operator_id')->nullable()->unsigned()->change();
I also tried this
$table->integer('operator_id')->unsigned()->default(null);
How do I make operator_id foreign key default to null?
If the data on your database is not important you could refresh your migrations and your database using
php artisan migrate:refresh
This will rollback and migrate all your migrations again. Make sure you wrote the down method right,
also, you migration should look like this:
public function up()
{
Schema::create('franchises', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('operator_id')->nullable();
$table->foreign('operator_id')->references('id')->on('operators')->onDelete('cascade')->onUpdate('cascade');
$table->string('case_number')->nullable();
$table->string('business_address')->nullable();
$table->date('date_granted')->nullable();
$table->date('expiry_date')->nullable();
$table->string('route_name')->nullable();
$table->string('deno')->nullable();
$table->integer('authorize_units')->nullable();
$table->string('remarks')->nullable();
$table->timestamps();
});
}
Other way to do it is creating a new migration like this:
public function up()
{
Schema::table('franchises', function (Blueprint $table) {
$table->unsignedInteger('operator_id')->nullable()->change();
});
}
I think you get this error because the record you are trying to insert contains a wrong value for the column operator_id. This value is not a correct operator id and is not null (it could be 0, "null" or empty string, ...)
Can you paste here the exact SQL query which rises this error ?
Add this inside up function and run php artisan migrate:refresh --seed
public function up(){
Schema::disableForeignKeyConstraints();
Schema::create('franchises', function (Blueprint $table) {
$table->increments('id');
$table->integer('operator_id')->unsigned()->nullable();
$table->foreign('operator_id')->references('id')->on('operators')->onDelete('cascade')->onUpdate('cascade');
$table->string('case_number')->nullable();
$table->string('business_address')->nullable();
$table->date('date_granted')->nullable();
$table->date('expiry_date')->nullable();
$table->string('route_name')->nullable();
$table->string('deno')->nullable();
$table->integer('authorize_units')->nullable();
$table->string('remarks')->nullable();
$table->timestamps();
});Schema::enableForeignKeyConstraints();}
you should use this
$table->foreign('operator_id')->references('id')->on('operators')->nullable()->onDelete('cascade')->onUpdate('cascade');

Php artisan migrate:refresh not working

This is users migration code:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('nickname')->unique();
$table->string('email')->unique();
$table->string('password', 60);
$table->string('pic_url')->nullable();
$table->string('language')->default('en');
$table->string('mobile_phone')->nullable();
$table->string('work_phone')->nullable();
$table->string('website')->nullable();
$table->string('twitter')->nullable();
$table->string('facebook')->nullable();
$table->string('description')->nullable();
$table->string('time_zone')->nullable();
$table->integer('rate_val')->nullable();
$table->integer('rate_count')->nullable();
$table->enum('role', array_keys(trans('globals.roles')))->default('person');
$table->enum('type', array_keys(trans('globals.type_user')))->default('normal');
$table->enum('verified', array_keys(trans('globals.verification')))->default('no');
$table->json('preferences')->nullable();
$table->rememberToken();
$table->timestamps();
$table->timestamp('disabled_at')->nullable();
$table->softDeletes();
});
}
"php artisan migrate:refresh" When I am trying to run this command I am getting error which is showing in image.
Please help me to resolve this issue.
Thanks in advance.
It seems there is known issue in laravel in some mysql versions please check here
For now if you want to resolve this issue use 'text' instead of json

Resources