PHP Developers Network
http://forums.devnetwork.net/

unable to create foreign key in laravel.
http://forums.devnetwork.net/viewtopic.php?f=72&t=143358
Page 1 of 1

Author:  gautamz07 [ Sun Feb 12, 2017 12:20 pm ]
Post subject:  unable to create foreign key in laravel.

I have the following migration for my tags table:

Syntax: [ Download ] [ Hide ]
Schema::create('tags', function (Blueprint $table) {
         $table->increments('id');
            // $table->mediumText('tag');
         $table->char('tag' , 15);
 });
 


Then i have the following migration for my admin table:

Syntax: [ Download ] [ Hide ]
public function up()
    {
        Schema::create('admin', function (Blueprint $table) {
            $table->increments('id');
            $table->mediumText('title');
            $table->text('blog_content');
            $table->char('tag' , 15);
            $table->string('filePath');
            $table->string('slug');
            $table->timestamps();
        });

        Schema::table('admin', function (Blueprint $table) {
            $table->foreign('tag')->references('tag')->on('tags');
        });
    }
 


Now when i check the laravel docs the examples of foreign keys are all for primary keys:
see here

Does a foreign key always need to be a primary key ??

The tables i have created are of type "InnoDB" .. i get the following error:



Thank you.
Gautam.

Author:  Celauran [ Sun Feb 12, 2017 9:10 pm ]
Post subject:  Re: unable to create foreign key in laravel.

The field you're referencing needs to be indexed. Also, why wouldn't you reference the primary key? What's the thinking with having two CHAR(15) fields referencing each other?

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/