I have started writing a small personal project on Laravel 10. The problem I encountered is as follows:
public function up(): void { Schema::table('users', function (Blueprint $table) { $table->foreignUuid('role_id')->nullable()->constrained('roles')->change(); }); } /*** Reverse the migrations.*/ public function down(): void { Schema::table('users', function (Blueprint $table) { $table->foreignUuid('role_id')->nullable(false)->constrained('roles')->change(); }); } But when I run php artisan migrate, I get the following error - SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'role_id' (Connection: mysql, SQL: alter table users add role_id char(36 ) null).
Any suggestions on how to modify the columns correctly would be greatly appreciated.
You can try the following:
Schema::table('users', function (Blueprint $table) { $table->char('role_id', 36)->nullable()->constrained('roles')->change(); });Or you can use the original SQL statement:
DB::statement('ALTER TABLE users MODIFY role_id CHAR(36) NULL'); DB::statement('ALTER TABLE users ADD CONSTRAINT fk_users_role_id FOREIGN KEY (role_id) REFERENCES roles (id)');