【问题标题】:Laravel 7 : Making foreign key from non primary keyLaravel 7:从非主键制作外键
【发布时间】:2020-10-19 18:55:22
【问题描述】:

我有如下关系图erd

绿色高亮表示主键,黄色高亮表示外键,可见外键是非主键

我已经创建了这三个表,但是在迁移时遇到了这个错误

SQLSTATE[HY000]: General error: 1005 Can't create table `project-whirlpool`.`mother_meters` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `mother_meters` add constraint `mother_meters_assign_hrid_foreign` foreign key (`assign_hrid`) references `tenants` (`hrid`))

这是我的租户表

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTenantsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tenants', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('nid');
            $table->string('nid_img');
            $table->string('phone');
            $table->string('exp_rent');
            $table->string('paid_rent');
            $table->string('dues');
            $table->string('pay_date');
            $table->string('comment');
            $table->integer('hrid');//home or room number
            $table->boolean('status');
            $table->date('exit');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tenants');
    }
}

母表

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateMotherMetersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */

    //protected $fillable = ['meter_number','hrid','type','consume_unit','bill_amount','year','month','pay_status'];

    public function up()
    {
        Schema::create('mother_meters', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('meter_number');
            $table->unsignedInteger('assign_hrid'); //home or room number
            $table->string('type');
            $table->string('consume_unit');
            $table->string('bill_amount');
            $table->string('year');
            $table->string('month');
            $table->string('pay_status');
            $table->timestamps();


            $table->foreign('assign_hrid')->references('hrid')->on('tenants');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('mother_meters');
    }
}

子仪表表

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSubMetersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */

    //protected $fillable = ['meter_number','hrid','type','prev_reading','curr_reading','consume_unit','bill_amount','year','month','pay_status'];

    public function up()
    {
        Schema::create('sub_meters', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('assign_meter_num');
            $table->unsignedInteger('rid');//home or room number
            $table->string('type');
            $table->string('prev_reading');
            $table->string('curr_reading');
            $table->string('consumeny_unit');
            $table->string('bill_amount');
            $table->string('year');
            $table->string('month');
            $table->string('pay_status');
            $table->timestamps();

            $table->foreign('assign_meter_num')->references('meter_number')->on('mother_meters');
            $table->foreign('rid')->references('assign_hrid')->on('mother_meters');

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('sub_meters');
    }
}

【问题讨论】:

  • 你有id那么为什么你需要hrid在同一张桌子上?
  • hrid 平均家庭/房间号

标签: eloquent laravel-artisan laravel-7 artisan-migrate


【解决方案1】:

在您的租户表中 做这样的事情:

$table->integer('hrid')->unique()

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 2022-01-07
    • 2013-08-28
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多