Do you have the RefreshDatabase trait on your TestCase.php class?
Weird migration problem while testing
Hi, I'm implementing tests in my app and I've got this weird situation.
I've done several change to my users tables with using migration obviously.
When I try a php artisan migrate:fresh everything is working.
On local env, User::factory()->create() works too but in this test I've got an error this specific error General error: 1 table users has no column named firstname (SQL: insert into "users" ("firstname", "lastname", "phone", "email", "email_verified_at", "address", "address_1", "zip_code", "city", "password", "updated_at", "created_at") values (David, Martinez, +33 7 90 93 44 45, [email protected], 2014-08-04 00:00:00, 167, rue Maurice, ?, 07599, Denisboeuf, $2y$04$amnt7y7.ydQ0XPCE2nK0GuKj6ftiHqnUVkODSuQC8Yo3djmA4iN.K, 2022-08-25 13:37:47, 2022-08-25 13:37:47))
I've added a var_dump on the up method in the migration (I can see yhe up method is called in the test)
The test:
test('admins can browses clients', function() {
$clients = User::factory()->create();
$this->get(route('clients.index'))
->assertSuccessful()
->assertSee($clients[0]->firstname)
->assertSee($clients[1]->firstname);
});
In order to debug I've added this line of code in my test
dd(\Illuminate\Support\Facades\Schema::getColumnListing("users"));
But the results show there is no "firstname" column (wtf ?)
Here is the latest migration for the users table
return new class extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['name']);
$table->string('firstname')->after('id');
$table->string('lastname')->after('firstname');
$table->string('phone', 20)->after('lastname');
$table->string('address')->nullable()->default(null)->after('email_verified_at');
$table->string('address_1')->nullable()->default(null)->after('address');
$table->string('zip_code', 10)->nullable()->default(null)->after('address_1');
$table->string('city')->nullable()->default(null)->after('zip_code');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->string('name')->after('id');
$table->dropColumn(['firstname', 'lastname', 'phone', 'address', 'address_1', 'zip_code', 'city']);
});
}
};
Is there anything I'm missing ? A typo ? A can't see what's the problem.
All other tests are working great
thanks
@Ashraam yes I see that. I think your problem is dropping and modifying multiple columns in the migration when using SQLite.
Dropping or modifying multiple columns within a single migration while using an SQLite database is not supported.
Please or to participate in this conversation.