よくある質問私は推測しましたが、私がオンラインで見つけた情報にもかかわらず解決できません。Eloquent - 外部キー制約を追加できません
私は、hasManyの関係のシーケンスをしました:
- ユーザーはが多く材料を持って多く契約を結んでいる多くののクライアントを持っています。
私は自分のベストを尽くしたが、私自身は、このエラーに直面見つける:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: create tableclients
(id
int unsigned not null,user_id
int unsigne d null, ...) default character set utf8mb4 collate
utf8mb4_unicode_ci engine = InnoDB)SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
移行:私は間違って
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->engine = 'InnoDB';
// Keys
$table->increments('id');
// Other
...
$table->timestamps();
});
}
public function down()
{
Schema::disableForeignKeyConstraints();
Schema::dropIfExists('clients');
Schema::drop('users');
Schema::enableForeignKeyConstraints();
}
}
class CreateClientsTable extends Migration
{
public function up()
{
Schema::create('clients', function (Blueprint $table) {
$table->engine = 'InnoDB';
// keys
$table->unsignedInteger('id')->unique();
$table->primary('id');
$table->unsignedInteger('user_id')
->nullable();
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade');
// others
...
});
}
public function down()
{
Schema::disableForeignKeyConstraints();
Schema::dropIfExists('contracts');
Schema::drop('clients');
Schema::enableForeignKeyConstraints();
}
}
class CreateContractsTable extends Migration
{
public function up()
{
Schema::create('contracts', function (Blueprint $table) {
$table->engine = 'InnoDB';
// Keys
$table->increments('id');
$table->string('contract_number')->unique();
$table->unsignedInteger('client_id');
$table->foreign('client_id')->references('id')->on('clients')
->onDelete('cascade');
// Others
...
});
}
public function down()
{
Schema::disableForeignKeyConstraints();
Schema::dropIfExists('materials');
Schema::drop('contracts');
Schema::enableForeignKeyConstraints();
}
}
class CreateMaterialsTable extends Migration
{
public function up()
{
Schema::create('materials', function (Blueprint $table) {
$table->engine = 'InnoDB';
// Keys
$table->increments('id');
$table->string('contract_number')->unique();
$table->unsignedInteger('contract_id');
$table->foreign('contract_id')->references('id')->on('contracts')
->onDelete('cascade');
// Others
...
});
}
public function down()
{
Schema::drop('materials');
}
}
何をしているのですか?
mysql/mariadbを使用していますか?そして、はいの場合、どのバージョンですか? – Jerodev
あなたの列は同じデータ型ですか? –
私はmysqlを使用しています。はい、彼らは... – Pixeuh