Laravel - Table 外键不起作用 -- 已更新
Laravel - Table with foreign key not working --Updated
我正在使用 Laravel 5.2 和 MySQL。
我正在开发一个项目来适应 Laravel。我的项目是一个电话簿,您可以在 table 上存储联系信息。但是要做到这一点,你需要登录系统,这是我使用 make:auth
命令创建的。看起来很简单。
在我的“Users
”table 中,我有字段 ID
。此 table 用于存储用户信息,以便他们可以登录并访问联系人资料。
在我存储联系人的“Contacts
”table 中,有一个名为“Created By
”的列应该采用字段 ID
来自用户 table,仅供参考谁是所述联系人的创建者。
但事情是这样的:
联系人 table 没有迁移,无论我做什么都没有关系。
我已经删除了两个 table 并从头开始制作它们,首先是 USERS
,因为它具有引用的主键,然后是 CONTACTS
,以及外部字段放。我什至放弃我的模型并使用上面相同的顺序创建它们,因为谁知道什么可能有效。
这是我的迁移文件:
用户迁移
----- ... -----
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email', 191)->unique();
$table->string('password', 191);
$table->timestamp('updated_at');
$table->timestamp('created_at');
$table->rememberToken();
});
}
----- ... -----
如前所述,table 具有我在另一个 table 中引用的 ID 字段。
联系人迁移
----- ... -----
public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id', false, true);
$table->foreign('user_id')->references('id')->on('users');
$table->string('image');
$table->string('name');
$table->string('lastname');
$table->string('email', 191)->unique();
$table->string('phone',191)->unique();
$table->string('address');
$table->string('description', 255);
});
----- ... -----
字段 user_id 引用了 table 用户中的 id 字段,如前所述。第二个参数将 incrementing 设置为 false,第三个参数将 unsigned 设置为 true。
即使我设置了默认样式 ($table->integer('user_id')->unsigned(); --- $table->foreign... ..;),迁移无效。
我什至在与主体分开的架构中创建了外部字段,如下所示:
Schema::table('contacts', function($table) {
$table->foreign('user_id')->references('id')->on('users');
});
但即使这样,迁移也无法正常进行。
真不知道怎么回事
这是我的联系人模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Contact extends Model
{
// Getting the user associated with the contact
// which means, I hope,
// The user that created the contact entry in the table 'contacts'
public function user()
{
return $this->belongsTo('App\User');
}
// protected $table = 'contacts';
// not using an id field
// could this be the error?
// dunno
// isn't laravel making an automatic id field?
// dunno too
public $fillable = array('name', 'lastname', 'email', 'phone', 'address');
// public $foreign = ('user_id');
//added because of error at migration
public $timestamps = false;
}
这是我的用户模型
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
// Putting it in simple terms
// Hopefully, this will return
// the many contacts that said user recorded in the 'contacts' table
public function contact()
{
return $this->hasMany('App\Contact');
}
// protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
更新
我刚刚回滚我的迁移并再次使用 migrate
命令,这次成功了。
我可以注册一个用户,所以 auth::check
可以工作,我可以看到在 CONTACTS
table.
中插入联系人的表单视图
但是当我点击保存按钮时,出现如下错误:
2/2
QueryException in Connection.php line 729:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`phonebook`.`contacts`, CONSTRAINT `contacts_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`))
1/2
PDOException in Connection.php line 457:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`phonebook`.`contacts`, CONSTRAINT `contacts_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`))
我还是不知道怎么回事。
这些是您可以添加联系人和附加用户的一些方式。既然你解决了外键问题,那我就不说了
$user = \Auth::user();
$user->contact()->create([
'name' => $request->input('name'),
'lastname' => $request->input('lastname'),
'email' => $request->input('email'),
'phone' => $request->input('phone'),
'address' => $request->input('address')
]);
或
\App\Contact::create([
'name' => $request->input('name'),
'lastname' => $request->input('lastname'),
'email' => $request->input('email'),
'phone' => $request->input('phone'),
'address' => $request->input('address'),
'user_id' => \Auth::id()
]);
或
$contact = new \App\Contact;
$contact->name = $request->input('name');
$contact->lastname = $request->input('lastname');
$contact->email = $request->input('email');
$contact->phone = $request->input('phone');
$contact->address = $request->input('address');
$contact->user_id = \Auth::id();
$contact->save();
当您批量分配值(上面显示的第二个选项)时,还要将 user_id
添加到 $fillable
属性。
我正在使用 Laravel 5.2 和 MySQL。
我正在开发一个项目来适应 Laravel。我的项目是一个电话簿,您可以在 table 上存储联系信息。但是要做到这一点,你需要登录系统,这是我使用 make:auth
命令创建的。看起来很简单。
在我的“Users
”table 中,我有字段 ID
。此 table 用于存储用户信息,以便他们可以登录并访问联系人资料。
在我存储联系人的“Contacts
”table 中,有一个名为“Created By
”的列应该采用字段 ID
来自用户 table,仅供参考谁是所述联系人的创建者。
但事情是这样的:
联系人 table 没有迁移,无论我做什么都没有关系。
我已经删除了两个 table 并从头开始制作它们,首先是 USERS
,因为它具有引用的主键,然后是 CONTACTS
,以及外部字段放。我什至放弃我的模型并使用上面相同的顺序创建它们,因为谁知道什么可能有效。
这是我的迁移文件:
用户迁移
----- ... -----
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email', 191)->unique();
$table->string('password', 191);
$table->timestamp('updated_at');
$table->timestamp('created_at');
$table->rememberToken();
});
}
----- ... -----
如前所述,table 具有我在另一个 table 中引用的 ID 字段。
联系人迁移
----- ... -----
public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id', false, true);
$table->foreign('user_id')->references('id')->on('users');
$table->string('image');
$table->string('name');
$table->string('lastname');
$table->string('email', 191)->unique();
$table->string('phone',191)->unique();
$table->string('address');
$table->string('description', 255);
});
----- ... -----
字段 user_id 引用了 table 用户中的 id 字段,如前所述。第二个参数将 incrementing 设置为 false,第三个参数将 unsigned 设置为 true。
即使我设置了默认样式 ($table->integer('user_id')->unsigned(); --- $table->foreign... ..;),迁移无效。
我什至在与主体分开的架构中创建了外部字段,如下所示:
Schema::table('contacts', function($table) {
$table->foreign('user_id')->references('id')->on('users');
});
但即使这样,迁移也无法正常进行。
真不知道怎么回事
这是我的联系人模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Contact extends Model
{
// Getting the user associated with the contact
// which means, I hope,
// The user that created the contact entry in the table 'contacts'
public function user()
{
return $this->belongsTo('App\User');
}
// protected $table = 'contacts';
// not using an id field
// could this be the error?
// dunno
// isn't laravel making an automatic id field?
// dunno too
public $fillable = array('name', 'lastname', 'email', 'phone', 'address');
// public $foreign = ('user_id');
//added because of error at migration
public $timestamps = false;
}
这是我的用户模型
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
// Putting it in simple terms
// Hopefully, this will return
// the many contacts that said user recorded in the 'contacts' table
public function contact()
{
return $this->hasMany('App\Contact');
}
// protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
更新
我刚刚回滚我的迁移并再次使用 migrate
命令,这次成功了。
我可以注册一个用户,所以 auth::check
可以工作,我可以看到在 CONTACTS
table.
但是当我点击保存按钮时,出现如下错误:
2/2
QueryException in Connection.php line 729:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`phonebook`.`contacts`, CONSTRAINT `contacts_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`))
1/2
PDOException in Connection.php line 457:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`phonebook`.`contacts`, CONSTRAINT `contacts_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`))
我还是不知道怎么回事。
这些是您可以添加联系人和附加用户的一些方式。既然你解决了外键问题,那我就不说了
$user = \Auth::user();
$user->contact()->create([
'name' => $request->input('name'),
'lastname' => $request->input('lastname'),
'email' => $request->input('email'),
'phone' => $request->input('phone'),
'address' => $request->input('address')
]);
或
\App\Contact::create([
'name' => $request->input('name'),
'lastname' => $request->input('lastname'),
'email' => $request->input('email'),
'phone' => $request->input('phone'),
'address' => $request->input('address'),
'user_id' => \Auth::id()
]);
或
$contact = new \App\Contact;
$contact->name = $request->input('name');
$contact->lastname = $request->input('lastname');
$contact->email = $request->input('email');
$contact->phone = $request->input('phone');
$contact->address = $request->input('address');
$contact->user_id = \Auth::id();
$contact->save();
当您批量分配值(上面显示的第二个选项)时,还要将 user_id
添加到 $fillable
属性。