Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
eleumasc opened this issue Jan 4, 2024 · 2 comments
Closed

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

eleumasc opened this issue Jan 4, 2024 · 2 comments

Comments

@eleumasc
Copy link

eleumasc commented Jan 4, 2024

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)

@driesvints
Copy link
Member

Heya, thanks for reporting.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here? Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Please do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

@eleumasc
Copy link
Author

eleumasc commented Jan 4, 2024

After investigating better, we have discovered that Blueprint creates a unique index in a separate table. In particular, you can list the indexes for the products table with the following statement: PRAGMA index_list('products');. Also, the driver enforces the unique constraint correctly.
We close the issue. Sorry for the false alarm and thanks for your time.

@eleumasc eleumasc closed this as completed Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants