0

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

4
  • 1
    unsignedBigInteger but you set default to empty string? try use 0 instead. Commented May 1, 2022 at 12:39
  • @BagusTesa problem is something else . read the text please Commented May 1, 2022 at 12:43
  • sure, it says "General error: 1 Cannot add a NOT NULL column with default value NULL." Commented May 1, 2022 at 13:22
  • @connectaadmin it would be faster for you to try his suggestion than arguing it. If you want to add a new column in an existing table with entries, either set a valid default or set it nullable. Commented May 1, 2022 at 13:29

1 Answer 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();
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.