Php 外键约束的格式不正确是一个问题
我在尝试迁移外键时遇到此错误Php 外键约束的格式不正确是一个问题,php,laravel,migration,Php,Laravel,Migration,我在尝试迁移外键时遇到此错误 Schema::create('people', function (Blueprint $table) { $table->id(); $table->timestamps(); $table->unsignedBigInteger('PersonId'); $table->integer('PersonRole'); $table->string('Pe
Schema::create('people', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->unsignedBigInteger('PersonId');
$table->integer('PersonRole');
$table->string('PersonName');
$table->string('Email')->unique();
$table->string('Password');
$table->string('Address');
$table->string('Gender');
$table->date('DoB');
});
Schema::create('flowers', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->unsignedBigInteger('FlowerId');
$table->string('Category');
$table->string('FlowerName');
$table->integer('FlowerPrice');
$table->string('Description');
$table->string('FlowerImage');
});
Schema::create('transactions', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->unsignedBigInteger('TransactionId');
$table->dateTime('Time');
$table->integer('PersonId')->unsigned();
});
Schema::table('transactiondetails', function (Blueprint $table) {
$table->foreign('FlowerId')->references('FlowerId')->on('Flowers')->onUpdate('cascade')->onDelete('cascade');
$table->foreign('TransactionId')->references('TransactionId')->on('Transactions')->onUpdate('cascade')->onDelete('cascade');
});
Schema::table('transactions', function (Blueprint $table) {
$table->foreign('PersonId')->references('PersonId')->on('People')->onUpdate('cascade')->onDelete('cascade');
});
我试图更改迁移顺序,并确保所有变量具有相同的数据类型
请帮我解决这个问题
谢谢如果确实要为非主键创建外键,则该外键必须是具有唯一约束的列 因此,在您的情况下,如果您将
FlowerId
&PersonId
设置为唯一的,它将被允许作为MySQL规则。如果你不能应用一个独特的约束,那你就不走运了,但是如果你仔细想想,这确实是有意义的
如果您有一个非常好的主键,为什么不使用它:
Schema::table('transactiondetails', function (Blueprint $table) {
$table->foreign('FlowerId')->references('id')->on('Flowers')->onUpdate('cascade')->onDelete('cascade');
$table->foreign('TransactionId')->references('id')->on('Transactions')->onUpdate('cascade')->onDelete('cascade');
});
Schema::table('transactions', function (Blueprint $table) {
$table->foreign('PersonId')->references('id')->on('People')->onUpdate('cascade')->onDelete('cascade');
});
试试这个
外键是一列或列的组合,其值与另一个表中的主键匹配。(因此您不使用主键)
如果不首先实际定义列,则不能指示列是外键。通常先定义它,然后将其指定为外键。但是,从第7版开始,您也可以使用异化
Schema::table('transactiondetails', function (Blueprint $table) {
$table->foreignId('FlowerId')->constrained('Flowers')->onUpdate('cascade')->onDelete('cascade');
$table->foreignId('TransactionId')->constrained('Transactions')->onUpdate('cascade')->onDelete('cascade');
});
Schema::table('transactions', function (Blueprint $table) {
$table->foreignId('PersonId')->constrained('People')->onUpdate('cascade')->onDelete('cascade');
});
Schema::table('transactiondetails', function (Blueprint $table) {
$table->foreignId('FlowerId')->constrained('Flowers')->onUpdate('cascade')->onDelete('cascade');
$table->foreignId('TransactionId')->constrained('Transactions')->onUpdate('cascade')->onDelete('cascade');
});
Schema::table('transactions', function (Blueprint $table) {
$table->foreignId('PersonId')->constrained('People')->onUpdate('cascade')->onDelete('cascade');
});