Search code examples
mysqllaravelmigration

Migration failing in laravel application


i'm trying to run som migration to my mysqlk database, the default ones created by laravel runs fine problem happens when it starts to run queries for my own tables Specifically the first table migration looks like this:

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('polls', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string('title')->nullable(false);
            $table->string('slug')->unique()->after('title')->nullable(false);
            $table->text('description')->nullable(false);
            $table->json('questions');
            $table->integer('max_points', false, true);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('polls');
    }
};

when i run php artisan migrate it gives the following error

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'after title, description text not null, questions json not null, max_p...' at line 1 (Connection: mysql, SQL: create table polls (idbigint unsigned not null auto_increment primary key,created_attimestamp null,updated_attimestamp null,titlevarchar(255) not null,slugvarchar(255) not null aftertitle, descriptiontext not null,questionsjson not null,max_points` int unsigned not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

10 database/migrations/2023_05_18_085528_create_polls_table.php:14

what am i doing wrong?


Solution

  • You shouldn't include after() when creating a table, only when altering it. During creation, the order of columns is determined by the order in which they appear in the migration.

    CREATE TABLE syntax does not include the AFTER modifier. It might be worth opening a Laravel issue to have this caught in the framework rather than trying to send an invalid command to the database.

    As well, columns are NOT NULL by default, calling nullable(false) is unneeded.