i've a migration like below
public function up()
{
Schema::create('live_classes', function (Blueprint $table) {
$table->bigInteger('time');
});
}
public function down()
{
Schema::dropIfExists('live_classes');
}
i wanted to change the datatype of the column time
from bigInteger
to date
.i tried like below.
public function up()
{
Schema::table('live_classes', function (Blueprint $table) {
$table->date('time')->nullable()->change();
});
}
public function down()
{
Schema::table('live_classes', function (Blueprint $table) {
$table->bigInteger('time');
});
}
This is not working.it shows the below error.How can i fix this?
Illuminate\Database\QueryException
SQLSTATE[42804]: Datatype mismatch: 7 ERROR: column "time" cannot be cast automatically to type date
HINT: You might need to specify "USING "time"::date". (SQL: ALTER TABLE live_classes ALTER "time" TYPE DATE)
at C:\wamp\www\Flotilla\Eustard-customers-Laravel-API-\vendor\laravel\framework\src\Illuminate\Database\Connection.php:703
699▕ // If an exception occurs when attempting to run a query, we'll format the error
700▕ // message to include the bindings with SQL, which will make this exception a
701▕ // lot more helpful to the developer instead of just the database's errors.
702▕ catch (Exception $e) {
➜ 703▕ throw new QueryException(
704▕ $query, $this->prepareBindings($bindings), $e
705▕ );
706▕ }
707▕ }
1 C:\wamp\www\Flotilla\Eustard-customers-Laravel-API-\vendor\laravel\framework\src\Illuminate\Database\Connection.php:492
PDOException::("SQLSTATE[42804]: Datatype mismatch: 7 ERROR: column "time" cannot be cast automatically to type date
HINT: You might need to specify "USING "time"::date".")
2 C:\wamp\www\Flotilla\Eustard-customers-Laravel-API-\vendor\laravel\framework\src\Illuminate\Database\Connection.php:492
PDOStatement::execute()
CodePudding user response:
You need to install package doctrine/dbal migrations in laravel,in order to makes changes in laravel migrations By using command
composer require doctrine/dbal
After that create new migration and use your above code
CodePudding user response:
I have make changes like this and write down your laraver version whatever you have like this "7.29.*"
CodePudding user response:
And you have made mistake in your down function, your down and up function should have same code,you have put biginteger in your down funtion that is another mistake,it should be like this
public function up()
{
Schema::table('live_classes', function (Blueprint $table) {
$table->time('time')->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('live_classes', function (Blueprint $table) {
$table->time('time')->change();
});
}
CodePudding user response:
You can create a new migration and change just one column type:
public function up()
{
Schema::table('sometable', function (Blueprint $table) {
$table->date('time')->change();
});
}
You need to install doctrine/dbal to make this work
composer require doctrine/dbal