PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Wed Dec 13, 2017 1:48 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Sun Feb 12, 2017 12:20 pm 
Offline
Forum Contributor
User avatar

Joined: Wed May 14, 2014 12:18 pm
Posts: 331
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 https://laravel.com/docs/5.0/schema#foreign-keys

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:

http://i.imgur.com/aRaln7s.png

Thank you.
Gautam.


Top
 Profile  
 
PostPosted: Sun Feb 12, 2017 9:10 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6402
Location: Montreal, Canada
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?

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group