number6's avatar

During Testing Only: SQLSTATE[42S22]: Column not found: 1054 Unknown column

I have a migration where I add a column to a table like so:

public function up()
  {
    Schema::table('applications', function (Blueprint $table) {
      $table->string('state')->after('template_id')->default(''); // in progress, in review, etc.
      $table->index(['team_id'], 'app_team_idx');
    });
  }
  public function down()
  {
    Schema::table('applications', function (Blueprint $table) {
      $table->dropIndex('app_team_idx');
    });

    Schema::table('applications', function (Blueprint $table) {
      $table->dropColumn('state');
    });
  }

During my tests, I run an artisan db:seed command. Half way through this seed, It updates the "state" field in an application. However it fails with:

Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'state' in 'field list' (SQL: update `applications` set `state` = dr, `applications`.`updated_at` = 2021-11-15 14:28:56 w
here `id` = 2)

I added a saving event listener on the model and printed the columns available in the table. The 'state' column is present, so I can only imagine this has something to do with the mysql & sqlite difference during testing. Usually it's a problem with the actual migration, but this shows the column exists. So what's happening?

static::saving(function($app) {
      $table = $app->getTable();
      $columns  = Schema::getColumnListing($table);
      var_dump($columns);
    });
0 likes
3 replies
Tray2's avatar

I seem to remember reading somewhere that ->after wasn't supported by SQLite so try removing it and just place the column last.

1 like
number6's avatar

@Tray2 Tried that just now; didn't work. I also have ->after on a handful of other migrations so I am guessing it is just ignored on sqlite migrations

number6's avatar

@Tray2 Not sure what happened. But the migration issues started happening in the main app after I deleted the test & dev dbs.

Please or to participate in this conversation.