Skip to content

$table->unique() does not work with SQLite driver #49581

Closed
@eleumasc

Description

@eleumasc

Laravel Version

10.39.0

PHP Version

8.3.1

Database Driver & Version

SQLite 3.40.0 for Windows 10 x64

Description

When creating a table using Blueprint (migrations), $table->unique() is ineffective at specifying an unique index. This happens with the SQLite driver that we are using for _development.

Steps To Reproduce

  1. Run php artisan make:migration create_products_table in a terminal
  2. Write to ./database/migrations/*_create_products_table.php:
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
  /**
   * Run the migrations.
   */
  public function up(): void
  {
    Schema::create('products', function (Blueprint $table) {
      $table->id();
      $table->string('code', 20);
      $table->string('name', 50);
      $table->timestamps();

      $table->unique('code');
    });

    // Get the raw SQL query for SQLite
    echo DB::select("SELECT sql FROM sqlite_master WHERE type='table' AND name='products'")[0]->sql;
  }

  /**
   * Reverse the migrations.
   */
  public function down(): void
  {
    Schema::dropIfExists('products');
  }
};
  1. Run php artisan migrate

The creation query written to stdout does not include the specification of the unique index on the "code" column. Also, if you open the database using a SQLite client, the "code" column is not marked as unique. We expect the opposite.

Stdout: CREATE TABLE "products" ("id" integer primary key autoincrement not null, "code" varchar not null, "name" varchar not null, "created_at" datetime, "updated_at" datetime)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions