Correct approach to making down migration for making column nullable type - laravel

I have a migration defined like so:
Schema::table('campaigns', function (Blueprint $table) {
$table->string('rates')->nullable(true)->change();
});
So I'm making "rates" nullable.
How should I handle the reverse migration? If there's no data in that column do I just set a default? I can't make it not null because that would violate integrity constraint. Can I not do anything about it?

Related

Laravel migration mix up with person/people table names

For some reason Laravel is getting mixed up pluralizing person/persons some reason it assumes my table is people.
I know how to get around this without manually specify the table name inside the $table->foreignId('person_id')->constrained('persons') but I wanted to know why it happens? And which tables names to avoid/watch out for.
Can't create table
person_logs (errno: 150 "Foreign key constraint is incorrectly
formed") (SQL: alter table person_logs add constraint
person_logs_person_id_foreign foreign key (person_id) references
people (id))
Schema::create('persons', function (Blueprint $table) {
$table->id('id');
$table->string('firstname');
$table->string('lastname');
});
Schema::create('person_logs', function (Blueprint $table) {
$table->id('id');
$table->string('example');
$table->foreignId('person_id')->constrained();
});
Down the hood it calls Str::plural() on person as part of person_id, the plural version of this is people. So the correct naming according to the Laravel convention for your persons table is actually people.
So change the table to people or constrained() first parameter is the table name.
$table->foreignId('person_id')->constrained('persons');

laravel6: how to change references of a foreign key with migration

I have tried to change foreign key reference by dropforeign key.I think everything is ok but i get this error:
SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP
FOREIGN KEY teacher_schedule_calendars_product_id_foreign; check
that it exists (SQL: alter table teacher_schedule_calendars drop
foreign key teacher_schedule_calendars_product_id_foreign)
How can I solve it?
my migration code is :
Schema::table('teacher_schedule_calendars', function (Blueprint $table) {
$table->dropForeign(['product_id']);
$table->foreign('product_id')->references('id') ->on('courses')->onDelete('cascade');
});
firstly, I ave removed the foreign and then add references. but it did not work.
I'm assuming you created a foreign key on the product_id column in a previous migration and you simply want to renew it or replace it with a foreign key to another table. In this case, what you are attempting to do might be problematic due to the way how migrations work and how individual database grammatics translate the commands.
What you can do to mitigate the issue is splitting the commands into two Schema::table($table) blocks:
Schema::table('teacher_schedule_calendars', function (Blueprint $table) {
$table->dropForeign(['product_id']));
});
Schema::table('teacher_schedule_calendars', function (Blueprint $table) {
$table->foreign('product_id')->references('id')->on('courses')->onDelete('cascade');
});

Change the datatype in the column with data in laravel migration

This is the migration? i have to change the string data column into integer data column with existing data
public function up()
{
Schema::table('SYS_tenants' ,function (Blueprint $table){
$table->integer('tena_type')->unsigned()->nullable()->change();
$table->foreign('tena_type')->references('id')->on('account_types');
});
}
As per laravel Documentation you can create a new migration and do it like this:
Schema::table('SYS_tenants', function (Blueprint $table) {
$table->integer('tena_type')->unsigned()->nullable()->change();
});
Before modifying a column, be sure to add the doctrine/dbal dependency
to your composer.json file.
composer require doctrine/dbal
Reference: Laravel -> Database: Migrations-> Modifying Columns
You can use change method on the field which you want to change the type after setting the new field type.
public function up() {
Schema::table('SYS_tenants' ,function (Blueprint $table){
$table->string('tena_type')->change();
});
}
I supposed the migration which create the table has already call all requirement you need like unique, nullable and so on. You can call change method, by the way there isn't restriction about any modification you want to perform like add other mysql index on that field.
Do not forget to add doctrine/dbal in composer.json file
Migrations#Modifying Columns
Looks like what you have should work:
Schema::table('SYS_tenants' ,function (Blueprint $table){
$table->integer('tena_type')->unsigned()->nullable()->change();
});
Depending on your database you may need to cast the values to the new type: (for mysql: https://www.mysqltutorial.org/mysql-cast/)
I already use this Laravel Migration
$table->integer('tena_type')->unsigned()->nullable()->change();
But it doesn't work because, the data already in the table. In that case it can't change the datatype.I use this DB statement to change the datatype with data.it's working properly.
DB::statement("alter table SYS_tenants modify tena_type integer not null"):

Migration - Cannot Change Double Data Type Value

I have an existing table created with this migration code:
Schema::create('mytable', function (Blueprint $table) {
$table->increments('id');
$table->double('mycolumn',8,2)->unsigned()->default(0);
$table->timestamps();
});
Then I have created another migration file to adjust the value range of my mycolumn field with the migration file below.
Schema::table('mytable', function (Blueprint $table) {
$table->double('mycolumn',15,2)->unsigned()->default(0)->change();
});
However I am getting an error:
In DBALException.php line 283:
Unknown column type "double" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a li
st of all the known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database introspection then you might have forgott
en to register all database types for a Doctrine Type. Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getM
appedDatabaseTypes(). If the type name is empty you might have a problem with the cache or forgot some mapping information.
What am I missing?
the double cannot be changed the way you do for other types, you can fix it using Doctrine\DBAL\Type
You can fix it in this way:
use Doctrine\DBAL\Types\FloatType;
use Doctrine\DBAL\Types\Type;
public function up() {
if (!Type::hasType('double')) {
Type::addType('double', FloatType::class);
}
Schema::table('mytable', function($table) {
$table->double('mycolumn',15,2)->default(0)->change();
.....
});
}
You are missing this from the documentation
Only the following column types can be "changed": bigInteger, binary, boolean, date, dateTime, dateTimeTz, decimal, integer, json, longText, mediumText, smallInteger, string, text, time, unsignedBigInteger, unsignedInteger and unsignedSmallInteger.
So double cannot be changed.
I haven't tried but you can maybe use a RAW MySQL query like this, try it locally first of course:
DB::statement('alter table mytable modify mycolumn DOUBLE(15,2) DEFAULT 0');
I'm not sure if this will help, but you may use decimal or float instead of double in this case, though float will not work if you want a limited amount of decimal places.
So it will look like:
$table->decimal('mycolumn',15,2)->unsigned()->default(0)->change();

Laravel Schema Builder, setting field description

Is it possible to add a description/comment to the sql field with laravel's schema builder. Just like in drupal?
It turns out that you can add comments, but it doesn't seem to be documented. This Laracasts post shows how – by adding a "comment" property to the end of the line.
Using their example,
Schema::create('products', function(Blueprint $table)
{
$table->increments('id');
$table->string('product_name')->comment = "Product name column";
$table->timestamps();
});
}
As it turns out – just testing now – you can actually use the more typical function syntax for that, so for example,
$table->string('product_name')->comment('Product name column');
...similar to setting ->default(...) or ->nullable(). Some people might prefer that style for consistency.
This seems to work great as of Laravel 5 and using MySQL. It might be a recent improvement.
Descriptions / comments are not supported by the schema builder and probably won't in the future. You have to fall back to SQL:
Assuming you use MySQL
Schema::create('users', function(Blueprint $table){
$table->increments();
$table->text('username');
$table->text('password', 60);
});
DB::statement('ALTER TABLE `users` CHANGE `password` `password` VARCHAR(60) COMMENT 'password hash');

Resources