【发布时间】:2018-02-02 04:48:53
【问题描述】:
所以我有三个表。客户/站点/用户。客户有许多站点。一个客户有很多用户。用户/站点属于客户。很直接。遗憾的是,我们正在从认为使用字符串键是个好主意的人那里导入站点/客户端表的信息。
这是我的迁移 -
这是客户表:
Schema::create('clients', function (Blueprint $table) {
$table->string('id', 25)->primary();
$table->char('number', 6)->unique();
$table->string('name', 50)->unique();
$table->timestamps();
});
这是网站表:
public function up()
{
Schema::create('sites', function (Blueprint $table) {
$table->string('id', 25)->primary();
$table->string('name', 30)->unique();
$table->string('number', 10)->unique();
$table->string('address_1', 60);
$table->string('address_2', 60)->nullable();
$table->string('city', 30);
$table->string('state', 3);
$table->string('postal_code', 10);
$table->string('country', 30);
$table->string('weather_code')->nullable();
$table->decimal('dimension', 18, 0)->nullable();
$table->string('dimension_uom', 10)->nullable();
$table->string('client_id', 25);
$table->softDeletes();
$table->timestamps();
// Belongs To Client
$table->foreign('client_id')->references('id')->on('clients');
});
}
这是用户表:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->string('client_id', 25);
$table->rememberToken();
$table->timestamps();
// Belongs To Client
$table->foreign('client_id')->references('id')->on('clients');
});
}
但是,当我运行这些时,我收到以下错误:
SQLSTATE[HY000]:一般错误:1215 无法添加外键约束(SQL:alter table
users添加约束users_client_id_foreign外键(client_id)引用clients(id))SQLSTATE[HY000]:一般错误:1215 无法添加外键约束
如果我删除了对用户的外键尝试,但将其保留在站点中,它可以正常工作。我在两者中都在做完全相同的事情,但由于某种原因它在网站中而不是在用户中有效。知道为什么吗?
【问题讨论】:
-
您使用的是哪个数据库?
标签: laravel laravel-5.5