Devender Gupta
Devender Gupta

Reputation: 546

Cannot create foreign key

I want to create a foreign key that is string from table "stocks" to "rfids". The two tables are shown below. stocks table:

Schema::create('stocks', function (Blueprint $table) {            

        $table->increments('tag_no');                       
        $table->string('stock_type');

        $table->string('rfid');
        $table->foreign('rfid')->references('RFID_UID')->on('rfids');

        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');

        $table->timestamps();            
    });

rfids table:

Schema::create('rfids', function (Blueprint $table) {            

        $table->increments('id');
        $table->string('RFID_UID');

        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');

        $table->timestamps();
    });

When i use php artisan migrate it shows error.

SQLSTATE[HY000]: General error: 1005 Can't create table hardware.#sql-81c_c8 (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table stocks add constraint stocks_rfid_foreign foreign key (rfid) references rfids (RFID_UID))

Someone help me pls!

Upvotes: 0

Views: 47

Answers (2)

Jonas Staudenmeir
Jonas Staudenmeir

Reputation: 25906

The migration order is crucial. You have to create the referenced table (rfids) first.

You can order the migrations by changing the date/time in the filename.

Upvotes: 1

Devender Gupta
Devender Gupta

Reputation: 546

I changed the tables this way:

Schema::create('rfids', function (Blueprint $table) {            

            $table->increments('id');
            $table->string('RFID_UID');

            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users');

            $table->timestamps();
        });

Schema::create('stocks', function (Blueprint $table) {            

        $table->increments('tag_no');                       
        $table->string('stock_type');            
        $table->string('rfid');           

        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');

        $table->timestamps();            
    });

Instead of using foreign key i did the following while storing

public function store(Request $request)
    {
        //
        if(Auth::check()){
            if (Stock::where('tag_no','=',$request->input('tag_no'))->exists()) { 
                return back()->withInput()->with('errors', 'Tag number already used!');                
                }

                $rfid_tag = Rfid::where('id',"=",$request->input('tag_no'))->first();

                $stock = Stock::create([
                    'tag_no' => $request->input('tag_no'),
                    'stock_type' => $request->input('stock_type'),
                    'species' => $request->input('species'),
                    'rfid'=>$rfid_tag->RFID_UID,                                
                    'user_id' => Auth::user()->id
                ]);               

                if($stock){
                    return redirect()->route('stocks.index', ['stocks'=> $stock->tag_no])
                    ->with('success' , 'Stock created successfully');
                }                
        }        
        return back()->withInput()->with('errors', 'Error creating new Stock');        
    }

It works fine for me. But idk if its the right solution.

Upvotes: 0

Related Questions