2017-05-31 8 views
0

私はLaravel 5.2とMySQLを使用しています。Laravel - 外部キーが無効なテーブル - 更新済み

私はLaravelに慣れるためのプロジェクトを開発しています。私のプロジェクトは連絡先情報をテーブルに保存できる電話帳です。しかし、これを行うには、システムにログインする必要があります。私はmake:authコマンドを使用して作成しました。シンプルさ、そうだ。

私の "Users"テーブルには、フィールドIDがあります。このテーブルは、ログインして連絡先にアクセスできるようにユーザー情報を保存するために使用されます。私は私の連絡先の保管場所である私の「Contacts」テーブルでは、

は、ユーザーテーブルからフィールドIDを取ることになっている「Created By」という名前の列で、ただの生みの親だった誰参照する連絡先を言いました。ここ

しかし、事ある:

連絡先テーブルが移行されていない、それは私が何をするかは重要ではありません。

参照先のプライマリキーがあり、その後に外部フィールドが設定されているCONTACTSがあるため、最初にUSERSを両方のテーブルから削除してゼロから作成しました。私はモデルを落として、上記の同じ順序でモデルを作成しました。何がうまくいくか分かっているからです。

はここに私の移行ファイルです:

USERSの移行に述べたように

----- ... ----- 

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(); 
     }); 
    } 

----- ... ----- 

、テーブルには、私は私の他のテーブルで参照していますIDフィールドがあります。

CONTACTS MIGRATION

----- ... ----- 

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述べたように、表ユーザのidフィールドを参照します。 2番目のパラメータはfalseに設定され、3番目のパラメータはunsignedに設定されます。

デフォルトのスタイル($ 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', 
    ]; 
} 

UPDATE

私はちょうど私のマイグレーションをロールバックして再度migrateコマンドを使用して、それです今回は働いた。

ユーザーを登録することができますので、auth::checkが動作し、CONTACTSテーブルに連絡先を挿入するためのフォームが表示されます。

しかし、私は保存するボタンをクリックしたときに、私は次のエラーを取得する:

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`)) 

私はまだ何が起こっているか分かりません。

+0

具体的なエラーはありますか? – Rahul

+0

投稿にエラーが挿入されました、Rahul。 –

+0

保存したいモデルに既存のユーザーIDを追加したかどうか確認できますか?そして、$ table-> integer( 'user_id'、false、true);を '$ table-> integer( 'user_id'、false、true) - > unsigned();' –

答えて

1

これは、連絡先を追加してユーザーを添付する方法のいくつかです。あなたが外来の鍵の問題を解決したので、私はそれに対処しません。あなたは質量値(上図の2番目のオプションを)割り当てるとき

$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(); 

また$fillableプロパティにuser_idを追加します。

+0

ありがとう、@サンディー!第三の選択肢は私のために働く。 –

関連する問題