繁体   English   中英

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

[英]Use some of tables without prefix in laravel

我正在将我的数据库与 magento 合并,并为我的 laravel 表指定前缀“edu_”。 我需要设置与 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`))   

有什么办法可以解决问题???

分两步添加迁移:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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