【问题标题】:Creating Foreign key with string data type in laravel在 laravel 中创建具有字符串数据类型的外键
【发布时间】:2019-05-07 18:05:02
【问题描述】:

我有两张桌子。一个是students_attendances,另一个是students。现在我想在这两个表之间设置一个外键。而且我还有字符串数据类型,我想在其上设置外键,但它给了我错误。下面是我的代码。回顾一下...

//students_attendances 表

public function up()
    {
        Schema::create('students_attendances', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('class_id')->index()->unsigned()->nullable();
            $table->string('student_id');
            $table->string('first_name');
            $table->string('last_name');
            $table->string('attendance');
            $table->timestamps();

            $table->foreign('student_id')->references('student_id')->on('students')->onDelete('cascade');


        });
    }

//学生桌

 public function up()
    {
        Schema::create('students', function (Blueprint $table) {
            $table->increments('id');
            $table->string('student_id');
            $table->string('first_name');
            $table->string('last_name');
            $table->string('DOB');
            $table->integer('students_class_id')->index()->unsigned()->nullable();
            $table->string('gender');
            $table->string('blood_group');
            $table->string('religion');
            $table->string('photo_id');
            $table->string('student_address');
            $table->integer('student_phone_no');
            $table->string('guardian_name');
            $table->string('guardian_gender');
            $table->string('guardian_relation');
            $table->string('guardian_occupation');
            $table->integer('guardian_phone_no');
            $table->string('email')->unique();
            $table->string('NIC_no');
            $table->string('guardian_address');
//            $table->string('document_id');
            $table->string('username');
            $table->string('password');
            $table->timestamps();
        });
    }

【问题讨论】:

    标签: laravel


    【解决方案1】:

    在你的学生表中更改

     $table->string('student_id');
    

    $table->string('student_id')->unique();
    

    要用作外键,它必须是唯一的键。

    所以,新的迁移将是

    Schema::table('students', function (Blueprint $table) {
               $table->unique("student_id");
            });
    

    【讨论】:

    • 在哪个表中?
    • 现在给我以下错误 一般错误:1005 无法创建表 sms.#sql-3378_54 (errno: 150 "外键约束格式不正确") (SQL: alter表students_attendances添加约束students_attendances_student_id_foreign外键(student_id)在删除级联时引用studentsstudent_id
    • 语法错误或访问冲突:1072 表中不存在键列“student_id”(SQL:alter table students_attendances add unique students_attendances_s tudent_id_unique(student_id))
    • 我做了这样的事情public function up() { Schema::create('students_attendances', function (Blueprint $table) { $table->increments('id'); $table->integer('class_id')->index()->unsigned()->nullable(); $table->unique('student_id'); $table->string('first_name'); $table->string('last_name'); $table->string('attendance'); $table->timestamps(); $table->foreign('student_id')->references('student_id')->on('students')->onDelete('cascade'); }); }
    • @HasnainKahn 您的表已经创建或者您想要创建新表,如果已经创建上述解决方案将起作用,如果您正在重新生成,您只需要在第一次迁移中指定将创建表,您不需要添加第二次迁移。
    猜你喜欢
    • 2019-02-07
    • 1970-01-01
    • 2016-12-17
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    相关资源
    最近更新 更多