Создание внешнего ключа со строковым типом данных в laravel - PullRequest
0 голосов
/ 07 мая 2019

У меня есть две таблицы. Один из них - student_attendances, а другой - студенты. Теперь я хочу установить внешний ключ между этими двумя таблицами. А также у меня есть строковый тип данных, для которого я хочу установить внешний ключ, но он дает мне ошибку. Ниже мой код. Сделайте обзор этого ...

// Таблица 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();
        });
    }

1 Ответ

1 голос
/ 07 мая 2019

В таблице учеников измените

 $table->string('student_id');

на

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

Для использования в качестве внешнего ключа это должен быть уникальный ключ.

Итак,новая миграция будет

Schema::table('students', function (Blueprint $table) {
           $table->unique("student_id");
        });
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...