【发布时间】:2020-03-24 10:23:18
【问题描述】:
我在尝试进行迁移时遇到问题。我有一个事件表和一个颜色表。我已经尝试使用 Tinker 在这些表之间建立关系并且没有问题,但是当我使用 PhpmyAdmin 单击 id (查看它是什么字段)时,我无法做到,因为这些表之间没有链接。这是我的两次迁移:
第一次迁移:
//Event migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class EventList extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('events', function (Blueprint $table) {
$table->increments('id');
$table->string('nom', 45);
$table->integer('couleur_id')->unsigned()->index();
$table->integer('annee_cours_id')->unsigned()->index();
});
Schema::enableForeignKeyConstraints('events', function (Blueprint $table){
$table->foreign('couleur_id')->references('id')->on('couleurs');
$table->foreign('annee_cours_id')->references('id')->on('annee_cours');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('events');
}
}
第二次迁移:
//Color migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Couleur extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('couleurs', function (Blueprint $table){
$table->increments('id');
$table->string('nom', 45);
$table->string('code_hexa', 45);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('couleurs');
}
}
我实际上是在使用 PhpmyAdmin 概念(如下图所示)来更轻松地建立关系,但我想只使用代码来执行相同的操作。是否有解决方案可以在 Laravel 上轻松完成此操作而不使用 PhpmyAdmin 概念?
提前感谢您的回答。
Picture of a relationship example on PhpmyAdmin
编辑:使用php artisan migrate --pretend 后,我得到了一些描述迁移的确切内容的行。有输出:
QuadrimestreList: create table `quadrimestres` (`id` int unsigned not null auto_increment primary key, `nom` varchar(45) not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
EventHasTypeEvent: create table `event_has_type_events` (`Event_id` int unsigned not null, `type_event_id` int unsigned not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
EventHasTypeEvent: alter table `event_has_type_events` add index `event_has_type_events_event_id_index`(`Event_id`)
EventHasTypeEvent: alter table `event_has_type_events` add index `event_has_type_events_type_event_id_index`(`type_event_id`)
EventHasTypeEvent: SET FOREIGN_KEY_CHECKS=1;
EventList: create table `events` (`id` int unsigned not null auto_increment primary key, `nom` varchar(45) not null, `couleur_id` int unsigned not null, `annee_cours_id` int unsigned not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
EventList: alter table `events` add index `events_couleur_id_index`(`couleur_id`)
EventList: alter table `events` add index `events_annee_cours_id_index`(`annee_cours_id`)
EventList: SET FOREIGN_KEY_CHECKS=1;
EventHasQuadrimestre: create table `event_has_quadrimestres` (`Event_id` int unsigned not null, `quadrimestre_id` int unsigned not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
EventHasQuadrimestre: alter table `event_has_quadrimestres` add index `event_has_quadrimestres_event_id_index`(`Event_id`)
EventHasQuadrimestre: alter table `event_has_quadrimestres` add index `event_has_quadrimestres_quadrimestre_id_index`(`quadrimestre_id`)
EventHasQuadrimestre: SET FOREIGN_KEY_CHECKS=1;
EventHasLocal: create table `event_has_locals` (`Event_id` int unsigned not null, `local_id` int unsigned not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
EventHasLocal: alter table `event_has_locals` add index `event_has_locals_event_id_index`(`Event_id`)
EventHasLocal: alter table `event_has_locals` add index `event_has_locals_local_id_index`(`local_id`)
EventHasLocal: SET FOREIGN_KEY_CHECKS=1;
EventHasEnseignant: create table `event_has_enseignants` (`Event_id` int unsigned not null, `enseignant_id` int unsigned not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
EventHasEnseignant: alter table `event_has_enseignants` add index `event_has_enseignants_event_id_index`(`Event_id`)
EventHasEnseignant: alter table `event_has_enseignants` add index `event_has_enseignants_enseignant_id_index`(`enseignant_id`)
EventHasEnseignant: SET FOREIGN_KEY_CHECKS=1;
Enseignant: create table `enseignants` (`id` int unsigned not null auto_increment primary key, `nom` varchar(45) not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
Couleur: create table `couleurs` (`id` int unsigned not null auto_increment primary key, `nom` varchar(45) not null, `code_hexa` varchar(45) not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
AnneeCours: create table `annee_cours` (`id` int unsigned not null auto_increment primary key, `nom` varchar(255) not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
Horaire: create table `horaires` (`id` int unsigned not null auto_increment primary key, `nom` varchar(255) not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
Local: create table `locals` (`id` int unsigned not null auto_increment primary key, `nom` varchar(45) not null, `commentaire` varchar(255) not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
Reservation: create table `reservations` (`id` int unsigned not null auto_increment primary key, `numero_semaine` int not null, `date` datetime not null, `heure_debut` varchar(45) not null, `heure_fin` varchar(45) not null, `Event_id` int unsigned not null, `horaire_id` int unsigned not null, `local_id` int unsigned not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
Reservation: alter table `reservations` add index `reservations_event_id_index`(`Event_id`)
Reservation: alter table `reservations` add index `reservations_horaire_id_index`(`horaire_id`)
Reservation: alter table `reservations` add index `reservations_local_id_index`(`local_id`)
Reservation: SET FOREIGN_KEY_CHECKS=1;
TypeEvent: create table `type_events` (`id` int unsigned not null auto_increment primary key, `nom` varchar(45) not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
别担心其他表,它们的问题与“Event”和“Couleurs”表相同。我认为这是关于外键的问题,但我看不出问题出在哪里。输出有用吗?
编辑 2:好的,它看起来更好,但是当我查看 PhpmyAdmin 概念时,表之间没有关系。我不知道这是否正常,也许 PhpmyAdmin 不像 Laravel 迁移那样识别关系?对不起,如果我需要这么多帮助。
【问题讨论】:
-
如果您为此运行
php artisan migrate或migrate --pretend会发生什么?在尝试创建外键之前,您必须先创建coleurs表。两个小评论。类名应该描述它的作用。CreateColeursTable和CreateEventsTable。在删除时,您应该首先删除外键。回退此迁移将失败。 -
我用
php artisan migrate --pretend的输出编辑了我的帖子。不知道对你有没有帮助? -
是的。谢谢你。我应该在下面回答这个问题,而不是作为评论。但我们走了。您尝试在尚不存在的表上创建外键。进行新的迁移并将其命名为
AddForeignKeys。删除任何迁移的所有外键约束并将它们移动到此新迁移。--pretend让您了解将按时间顺序发生的事情。它不会显示矛盾的错误。是的,在down()函数中,删除所有创建的外键。祝你好运! -
看起来更好,但我在 PhpmyAdmin 概念中看不到任何关系。我不知道这是正常的还是有其他问题。对不起,如果我要求太多帮助。