nadj's avatar
Level 9

Testing with sqlite CONSTRAINT name issue

I'm updating from laravel 5.8 to 6 the issue is that all of my tests, that are depending on the database, are failing. The error I'm getting is:

Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 near "0": syntax error (SQL: CREATE TABLE posts (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, type_id INTEGER NOT NULL, author_id INTEGER NOT NULL, status INTEGER NOT NULL, "order" INTEGER DEFAULT NULL, published DATETIME NOT NULL, created_at DATETIME DEFAULT NULL, updated_at DATETIME DEFAULT NULL, template VARCHAR(255) DEFAULT NULL COLLATE BINARY, CONSTRAINT 0 FOREIGN KEY (type_id) REFERENCES post_types (id) ON UPDATE NO ACTION ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT 1 FOREIGN KEY (author_id) REFERENCES users (id) ON UPDATE NO ACTION ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE))

For some reason the sql generated from the migration creates CONSTRAINT 0, CONSTRAINT 1 which is not a valid constraint name in sqlite. Is there a fix for this? I'm struggling with it for a quite long time :(

0 likes
4 replies
nadj's avatar
Level 9

Here it is

<?php

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

class CreatePosts extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        Schema::create('post_types', function (Blueprint $table) {
            $table->increments('id');
            $table->string('type_name', 100);
            $table->tinyInteger('order');
            $table->timestamps();
        });

        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->smallInteger('status');
            $table->integer('order')->nullable();
            $table->integer('type_id')->unsigned()->index();
            $table->integer('author_id')->unsigned()->index();
            $table->dateTime('published');
            $table->timestamps();

            $table->foreign('author_id')
                ->references('id')->on('users')
                ->onDelete('cascade');

            $table->foreign('type_id')
                ->references('id')->on('post_types')
                ->onDelete('cascade');
        });

        Schema::create('post_translations', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('post_id')->unsigned()->index();
            $table->string('title');
            $table->text('description')->nullable();
            $table->text('content')->nullable();
            $table->string('slug')->nullable()->unique();
            $table->string('locale')->index();

            $table->unique(['post_id','locale']);
            $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('post_translations');
        Schema::drop('posts');
        Schema::drop('post_types');
    }
}

nadj's avatar
Level 9

Thank you, I missed that :(

Please or to participate in this conversation.