In my migration file , I am adding organization id to the user's table , i tried all of the code below but not solved yet
class AddOrganizationIdToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
// $table->foreignId('organization_id')->references('id')->on('organizations')->onDelete('cascade');
$table->foreignId('organization_id')->constrained('organizations')->cascadeOnDelete();
// $table->foreign('organization_id')->references('id')->on('organizations')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
//
});
}
CodePudding user response:
if you write with foreignId
need:
$table->foreignId('organization_id')->constrained('organizations')->cascadeOnDelete();
or just foreign
$table->foreign('organization_id')->references('id')->on('organizations')->onDelete('cascade');
CodePudding user response:
I have data on my user's table so it is giving errors while migrating, to solve it I add the field as nullable
$table->foreignId('organization_id')->nullable()->references('id')->on('organizations')->onDelete('cascade');