Я хочу определить внешний ключ между двумя или более таблицами

Я перепробовал много вещей, но это не сработало. Позвольте мне использовать то, что я сделал:

введите описание изображения здесь

И эта таблица:
введите описание изображения здесь

Я получаю эту ошибку:

SQLSTATE [HY000]: общая ошибка: 1005 Не удается создать таблицу cmpe,#sql-2068_94 (errno: 150 «ограничение внешнего ключа i
неправильно сформировано «)

Не могли бы вы мне помочь?

0

Решение

Все ваши внешние поля должны быть int вместо строки. Также сделайте их без знака. Это правильный формат для создания внешнего ключа.

Пример:

$table->integer('myForeignID')->unsigned();

Эти строки могут быть удалены, поскольку это, скорее всего, не требуется:

$table->foreign('year')->references('id')->on('course');
$table->foreign('semester')->references('id')->on('course');

Также рендеринг столов курса и инструкторов перед столами постов.

0

Другие решения

public function up()
{
Schema::create('posts', function (Blueprint $table) {

$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('instructors_id')->unsigned()->nullable();
$table->integer('course_id')->unsigned()->nullable();
$table->integer('year')->unsigned()->nullable();
$table->integer('semester')->unsigned()->nullable();
$table->string('body');
$table->integer('post_id');
$table->rememberToken();
$table->timestamps();

$table->foreign('instructors_id')->references('id')->on('instructors');
$table->foreign('course_id')->references('id')->on('course');
$table->foreign('year')->references('id')->on('course');
$table->foreign('semester')->references('id')->on('course');

});
}public function up()
{
Schema::create('course', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->integer('semester');
$table->integer('year');
});
}
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector