在laravel中使用一些没有前缀的表

时间:2015-12-07 07:41:04

标签: php laravel laravel-5.1

我正在使用magento合并我的数据库并指定前缀' edu _'对于我的laravel表。我需要设置与magento的客户表即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();
    });

在指定关系发货时,我收到错误,因为Laravel将customer_entity视为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`))   

有没有办法解决这个问题?

2 个答案:

答案 0 :(得分:0)

分两步添加迁移:

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');
    });
}

编辑:
如果您想与已存在的表创建关系,请在最后再创建一个迁移文件以指定所有关系。

例如:

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');
    });

答案 1 :(得分:0)

DB::raw()方法添加到on()关系方法中。像这样:

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();
    });