I have a column of type enum I want to change it to type varchar but it brings an error that the sql syntax is not correct, please what's the solution
This is the table creation migration code
class CreateCurrenciesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
$symbols = ['₦', '$', '£'];
Schema::create('currencies', function (Blueprint $table) use($symbols) {
$table->increments('id');
$table->string('name', 50);
$table->string('code', 5);
$table->enum('symbol', $symbols);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
DB::statement('SET FOREIGN_KEY_CHECKS = 0');
Schema::dropIfExists('currencies');
DB::statement('SET FOREIGN_KEY_CHECKS = 1');
}
This is the migration code I want to use to change the column type but keep on getting an error
class AddSymbolToImagesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('images', function (Blueprint $table) {
DB::statement('ALTER TABLE images ALTER COLUMN symbol VARCHAR(200)');
// $table->text('symbol')->change();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('images', function (Blueprint $table) {
// $symbols = ['₦', '$', '£'];
// $table->enum('symbol', $symbols)->change();
DB::statement('ALTER TABLE images ALTER COLUMN symbol enum ');
});
}
}
This does not really have anything to do with Laravel and you should also include the error next time.
To change the column type you can use MODIFY or MODIFY COLUMN.
So this would work:
ALTER TABLE images MODIFY symbol varchar(200)
There is a few to change the column name as you can see in the docs here: https://dev.mysql.com/doc/refman/8.0/en/alter-table.html#alter-table-redefine-column
Related
In my laravel appication I have two migrations.
2022_06_08_075452_create_schedule_types_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateScheduleTypesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('schedule_types', function (Blueprint $table) {
$table->increments('id');
$table->string('schedule_type_name');
$table->string('schedule_type_color');
$table->string('schedule_type_description');
$table->boolean('status')->default(0);
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('schedule_types');
}
}
2022_06_08_054916_create_schedules_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateSchedulesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('schedules', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('department_id');
$table->foreign('department_id')->references('id')->on('departments');
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->unsignedBigInteger('company_id');
$table->foreign('company_id')->references('id')->on('companies');
$table->unsignedBigInteger('added_by');
$table->foreign('added_by')->references('id')->on('users');
$table->string('schedule_name');
$table->integer('schedule_type_id');
$table->foreign('schedule_type_id')->references('id')->on('schedule_types');
$table->date('schedule_start_date')->nullable();
$table->date('schedule_end_date')->nullable();
$table->date('schedule_actual_end_date')->nullable();
$table->time('schedule_travel_time')->nullable();
$table->unsignedBigInteger('rotation_scheme_id');
$table->foreign('rotation_scheme_id')->references('id')->on('schedule_rotational');
$table->date('rotational_schedule_period_from')->nullable();
$table->date('rotational_schedule_period_to')->nullable();
$table->unsignedBigInteger('rotation_shift_id');
$table->foreign('rotation_shift_id')->references('id')->on('schedule_has_shift');
$table->timestamps();
$table->softDeletes();
$table->index([
'department_id', 'user_id', 'schedule_type_id', 'company_id', 'added_by','schedule_start_date',
'schedule_end_date', 'rotation_scheme_id', 'rotational_schedule_period_from', 'rotational_schedule_period_to',
'rotation_shift_id']);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('schedules');
}
}
But, wen I tried to run my migrations, I'm getting following error,
SQLSTATE[HY000]: General error: 1005 Can't create table
hrapp_dev.schedules (errno: 150 "Foreign key constraint is
incorrectly formed") (SQL: alter table schedules add constraint
schedules_schedule_type_id_foreign foreign key (schedule_type_id)
references schedule_types (id))
I initially thought this is occurring due to an int and bigint issue, but both are in int....
UPDATE
I ran the two migration files one at a time due to timestamp issue, but still getting the same issue...
Your foreign key schedule_type_id should be unsigned for the relationship to work. In your 2022_06_08_054916_create_schedules_table.php change to:
$table->integer('schedule_type_id')->unsigned();
Do it this way following the more cleaner syntax from Laravel 7.x:
class CreateSchedulesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('schedules', function (Blueprint $table) {
$table->bigIncrements('id');
$table->foreignId('department_id')->constrained();
$table->foreignId('user_id')->constrained();
$table->foreignId('company_id')->constrained();
$table->foreignOd('added_by')->constrained('users');
$table->string('schedule_name');
// Here is your problem the schedule_type_id was not correctly formed
$table->foreignId('schedule_type_id')->constrained();
$table->date('schedule_start_date')->nullable();
$table->date('schedule_end_date')->nullable();
$table->date('schedule_actual_end_date')->nullable();
$table->time('schedule_travel_time')->nullable();
$table->foreignId('rotation_scheme_id')->constrained('schedule_rotational');
$table->date('rotational_schedule_period_from')->nullable();
$table->date('rotational_schedule_period_to')->nullable();
$table->foreignId('rotation_shift_id')->constrained('schedule_has_shift');
$table->timestamps();
$table->softDeletes();
$table->index([
'department_id', 'user_id', 'schedule_type_id', 'company_id', 'added_by','schedule_start_date',
'schedule_end_date', 'rotation_scheme_id', 'rotational_schedule_period_from', 'rotational_schedule_period_to',
'rotation_shift_id']);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('schedules');
}
}
the foreignId('tablename_id`) creates a foreign key and constrained() will make it constrained using the column name to determine the table. If you have a different table name than the one used in column name then you can pass the table in constrained('table')
I'm trying to create a migration on Laravel 8, Here's my table
class CreateProductVariationOrderTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('product_variation_order', function (Blueprint $table) {
$table->integer('order_id')->unsigned()->index();
$table->integer('product_variation_id')->unsigned()->index();
$table->integer('quantity')->unsigned();
$table->timestamps();
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
$table->foreign('product_variation_id')->references('id')->on('product_variations')->onDelete('cascade');
});
}
public function down()
{
Schema::dropIfExists('product_variation_order');
}
}
And When I run php artisan migrate I get this chunk of error : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table product_variation_orderadd constraintproduct_variation_order_order_id_foreign foreign key (order_id) references orders (id) on delete cascade)
⚠️ EDIT: Here's my product_variation migration file.
class CreateProductVariationsTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('product_variations', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned()->index();
$table->string('name');
$table->integer('price')->nullable();
$table->integer('order')->nullable();
$table->timestamps();
$table->foreign('product_id')->references('id')->on('products');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('product_variations');
}
}
The foreign key has to be the same type as the reference key.
In your orders table, you define id as bigIncrements() which is unsigned big integer.
In your product_variation_order table, you define order_id as unsigned integer.
So the two keys do not match. Generally you should use big integer because it allows your database to grow larger and space difference between integer and big integer isn't significant.
So change order_id as unsigned big integer.
$table->unsignedBigInteger('order_id')->nullable();
Also for consistency, all keys should be bigIncrements() or unsignedBigInteger(), this will save you headache in the future.
Use this code:
product_variations:
class CreateProductVariationsTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('product_variations', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('product_id')->unsigned()->nullable();
$table->string('name');
$table->integer('price')->nullable();
$table->integer('order')->nullable();
$table->timestamps();
$table->foreign('product_id')->references('id')->on('products');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('product_variations');
}
}
product_variation_order:
class CreateProductVariationOrderTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('product_variation_order', function (Blueprint $table) {
$table->bigInteger('order_id')->unsigned()->nullable();
$table->bigInteger('product_variation_id')->unsigned()->nullable();
$table->integer('quantity')->unsigned();
$table->timestamps();
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
$table->foreign('product_variation_id')->references('id')->on('product_variations')->onDelete('cascade');
});
}
public function down()
{
Schema::dropIfExists('product_variation_order');
}
}
Sometimes it is necessary to migrate first the table in which there is the primary key before the table which contains the foreign key. To migrate a specific table use :
php artisan migrate --path=/database/migrations/file_migration_name.php
I have a column called owner_type in a table called events. This column is filled with one of two values: 0 (signalling regular users) and 1 (for business users).
This column has a data type string, I'd like to change it to integer given that it'll always contain 0s and 1s. So I did this:
public function up()
{
Schema::table('events', function (Blueprint $table) {
$table->integer('owner_type')->change()->default(0);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('events', function (Blueprint $table) {
$table->string('owner_type')->default(0)->change();
});
}
However, I receive this error:
n AbstractPlatform.php line 461:
Unknown database type enum requested,
Doctrine\DBAL\Platforms\MySQL57Platform may not support it.
In the migration that created events I have:
$table->enum('**', ['**', '**'])->default('**');
$table->enum('status', ['*', '*', '*', '*', '*'])->default('*');
However, this table is already migrated. So what's wrong?
Try this
public function up()
{
//
Schema::table('events', function($table) {
$table->string('owner_type')->default(0);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
Schema::table('events', function($table) {
$table->dropColumn('owner_type');
});
}
I'm having a problem with migration on my Laravel project.
Since i'm fairly new to Laravel I can't figure it out.
I want to add a foreign key to an already existing table and this works, but when I refresh my migrations I get this error:
[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key
constraint fails (SQL: drop table `battles`)
[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key
constraint fails
These are the migrations I currently have:
Table Projects
class CreateProjectsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('projects', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('body');
$table->string('tags');
$table->string('img');
$table->string('img_tricolor');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('projects');
}
}
Table Battles
class CreateBattlesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('battles', function (Blueprint $table) {
$table->increments('id');
$table->string('battle_theme');
$table->boolean('battle_active');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('battles');
}
}
Adding foreign key for battles in projects
class AddProjectsBattleIdFk extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('projects', function (Blueprint $table) {
$table->integer('battle_id')->unsigned();
$table->foreign('battle_id')->references('id')->on('battles')->onDelete('set null');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('projects', function (Blueprint $table) {
//
});
}
}
I suppose it has something to do with the battles table.
In down methods you need to remove foreign keys first:
In CreateProjectsTable
public function down()
{
Schema::table('projects', function (Blueprint $table) {
$table->dropForeign('projects_user_id_foreign');
});
Schema::drop('projects');
}
In AddProjectsBattleIdFk
public function down()
{
Schema::table('projects', function (Blueprint $table) {
$table->dropForeign('projects_battle_id_foreign');
$table->dropColumn('battle_id');
});
}
I had the same problem,the Marcin's answer worked but I also found that another option is using fresh instead of refresh, in that case you don't need to remove foreign keys.
I don't know if this is more updated solution will help someone that is facing the same problem:
use this in your migration down function
Schema::disableForeignKeyConstraints();
I usually create as the last migration or name it to be sorted as last (cause migration comand runs the files under migration folder by order) something like 2099_12_32_AlterTablesCreateForeignKeysmigration where in the up function I specify all my keys for every table and at the end enable foreign keys constraints Schema::enableForeignKeyConstraints(); and then then in the down i just disabled them Schema::disableForeignKeyConstraints(); to allow the reset to truncate the tables.
I have found this question very similar to mine Make column not nullable in a Laravel migration though it is almost 3 years old and it surely does not belong to Laravel 5
My problem is that I have a migration which in the up function it modifies a column to make it nullable.
Now, in the down function I want to make it not nullable again.
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('mytable', function(Blueprint $table) {
$table->string('mycolumn')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('mytable', function(Blueprint $table) {
/* THIS IS WHAT I WOULD EXPECT TO DO
THOUGH OF COURSE THE FUNCTION notNullable DOES NOT WORK */
$table->string('mycolumn')->notNullable()->change();
});
}
I could achieve that using raw SQL, but I would like to do it using Laravel methods if it is possible... but I couldn't find it, probably it hasn't been implemented it in the version 5 either.
To reset it back to not nullable. You may try this.
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('mytable', function(Blueprint $table) {
$table->string('mycolumn')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('mytable', function(Blueprint $table) {
/* By default it's NOT NULL */
$table->string('mycolumn')->nullable(false)->change(); // <--- here
});
}
By default its NOT NULL, so you should try this.
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('mytable', function(Blueprint $table) {
$table->string('mycolumn')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('mytable', function(Blueprint $table) {
/* By default it's NOT NULL */
$table->string('mycolumn')->change();
});
}