简体   繁体   中英

Use some of tables without prefix in laravel

I am merging my database with magento and specifying prefix 'edu_' for my tables of laravel. I need to set relationship with customer table of magento ie customer_entity.

Schema::create('article_comments', function (Blueprint $table) {
        $table->increments('id');
        $table->text('comment');
        $table->integer('user_id')->unsigned();
        $table->integer('last_modified_by')->unsigned();
        $table->string('ip_address')->nullable();
        $table->boolean('active')->default(1);
        $table->foreign('user_id')->references('entity_id')->on('customer_entity');
        $table->timestamps();
        $table->softDeletes();
    });

While specifying relation ship, I get error because Laravel treats customer_entity as edu_customer_entity.

Cannot add foreign key constraint (SQL: alter table `edu_article_comments` add constraint article_com  
  ments_user_id_foreign foreign key (`user_id`) references `edu_customer_entity` (`entity_id`))   

Is there any way to solve the issue ???

Add the migration in two steps:

public function up()
{
    Schema::create('article_comments', function (Blueprint $table) {
        $table->increments('id');
        $table->text('comment');
        $table->integer('user_id')->unsigned();
        $table->integer('last_modified_by')->unsigned();
        $table->string('ip_address')->nullable();
        $table->boolean('active')->default(1);
        $table->timestamps();
        $table->softDeletes();
    });

 Schema::table('article_comments', function (Blueprint $table) {
    $table->foreign('user_id')->references('entity_id')->on('customer_entity');
    });
}


If you wanna create a relationship with a table that already exists, create one more migration file in the end to specify all the relationships.

For example:

Schema::table('table', function(Blueprint $table) {
        $table->foreign('column')->references('column')->on('table')->onDelete('action');

    Schema::table('areas', function(Blueprint $table) {
        $table->foreign('column')->references('column')->on('tablename')->onDelete('action');
    });

Add DB::raw() method into on() relationship method. Like this:

Schema::create('article_comments', function (Blueprint $table) {
        $table->increments('id');
        $table->text('comment');
        $table->integer('user_id')->unsigned();
        $table->integer('last_modified_by')->unsigned();
        $table->string('ip_address')->nullable();
        $table->boolean('active')->default(1);
        $table->foreign('user_id')->references('entity_id')->on(DB::raw('customer_entity'));
        $table->timestamps();
        $table->softDeletes();
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM