codeigniter 4 migration file not working if having same suffix?

Viewed 233

I've just started learning about codeigniter 4. I'm trying to migrate my database but when I try to migrate the same table twice, it gives an error Cannot declare class App\Database\Migrations\Barang, because the name is already in use.

but none of my migration files have the same name as you can see in the image below enter image description here

1 Answers

Explanation:

When you run php spark make:migration barang, CodeIgniter 4 creates a migration file app/Database/Migrations/2022-03-22-135842_Barang.php having a class name Barang under the namespace App\Database\Migrations.

When you run php spark make:migration barang the second or third time, CodeIgniter 4 performs the same process as specified above except that the respective migration files will have different filenames more specifically different "timestamp prefixes".

When you now run php spark migrate to initiate a migration process, CodeIgniter 4 realizes that you have conflicting class names under the same namespace, hence the error.

Solution:

Always specify unique "migration names" when creating a new migration using the command:

php spark make:migration unique_migration_name_here

Most preferably, the "migration name" should briefly describe the actual intention of the migration. I.e

php spark make:migration alter_users_rename_profile_pic_add_timestamps

Check this answer I posted earlier:

codeigniter 4 migration file not creating table

Related