'SQLite error for migration without default value in Laravel
I have a Laravel project with a MySQL database, and my migration works perfectly, but my problem is when I change the MySQL connection to SQLite and run the migration, I get an error for fields that do not have a default value. What is the solution for this? I found this solution for that is dirty, and I have to add this condition to many migrations.
$driver = Schema::connection($this->getConnection())
->getConnection()->getDriverName();
Schema::table('proposals', function (Blueprint $table) use ($driver) {
if ($driver === 'sqlite') {
$table->unsignedBigInteger('final_amount')->default('');
} else {
$table->unsignedBigInteger('final_amount');
}
});
Error
SQLSTATE[HY000]: General error: 1 Cannot add a NOT NULL column with default value NULL
Solution 1:[1]
If you want to add a new column in an existing table with entries, either set a valid default or set it nullable
$driver = Schema::connection($this->getConnection())->getConnection()->getDriverName();
Schema::table('proposals', function (Blueprint $table) use ($driver){
if ($driver === 'sqlite'){
$table->unsignedBigInteger('final_amount')->default(0);
}else{
$table->unsignedBigInteger('final_amount')->nullable();
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | N69S |
