Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string column type to integer using Laravel migration?

Tags:

php

mysql

laravel

I am trying to alter the column datatype using Laravel migration. But I am facing following error. Please help me out.

Schema::table('files', function(Blueprint $table) {
    $table->integer('app_id')->change();
    $table->index(['app_id', 'filename']);
});

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CHARACTER SET utf8 DEFAULT 0 NOT NULL COLLATE utf8_unicode_ci' at line 1 (SQL: ALTER TABLE files CHANGE app_id app_id INT CHARACTER SET utf8 DEFAULT 0 NOT NULL COLLATE utf8_unicode_ci)

like image 224
Nagarajan Malaichamy Avatar asked Nov 21 '19 14:11

Nagarajan Malaichamy


People also ask

How do I change a column to unique in Laravel migration?

$table->unique('slug'); So you add unique index to existing 'slug'.

How varchar is defined in Laravel migration?

How varchar is defined in Laravel migration? Instead you need to define the maximum value of the field yourself. In order to define max length for a text field just provide max length limit as second parameter: $table->string('name', 64); It will result in a VARCHAR column being created in your MySQL database.


2 Answers

Your issue is most likely caused by a bug in the most recent version of the doctrine/dbal package. The issue was introduced with v2.10.0.

You can always downgrade the package in your composer.json to v2.9.3 it should work just fine.

See the offical issue here: https://github.com/doctrine/dbal/issues/3714

like image 115
Nicolas Buch Avatar answered Oct 11 '22 10:10

Nicolas Buch


As a alternative solution to https://github.com/doctrine/dbal/issues/3714 that is downgrading the doctrine/dbal package u can do:

Schema::table('member_section', function (Blueprint $table) {
    $table->bigInteger('type')->charset(null)->collation(null)->change();
});
like image 41
Digital87 Avatar answered Oct 11 '22 12:10

Digital87