Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8370467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:55:09+00:00 2026-06-09T13:55:09+00:00

Essentially, I am having the same issue as this guy, minus the table prefix.

  • 0

Essentially, I am having the same issue as this guy, minus the table prefix. Because I have no table prefix, his fix does not work. http://forums.laravel.com/viewtopic.php?id=972

I am trying to build a table using Laravel’s Schema Builder like this:

Schema::create('lessons', function($table)
{
    $table->increments('id');
    $table->string('title')->nullable();
    $table->string('summary')->nullable();
    $table->timestamps();
});

Schema::create('tutorials', function($table)
{
    $table->increments('id');
    $table->integer('author');
    $table->integer('lesson');
    $table->string('title')->nullable();
    $table->string('summary')->nullable();
    $table->string('tagline')->nullable();
    $table->text('content')->nullable();
    $table->text('attachments')->nullable();
    $table->timestamps();
});

Schema::table('tutorials', function($table)
{
    $table->foreign('author')->references('id')->on('users');
    $table->foreign('lesson')->references('id')->on('lessons');
});

The issue is, when I run this code (in a /setup route), I get the following error:

SQLSTATE[HY000]: General error: 1005 Can't create table 'tutorials.#sql-2cff_da' (errno: 150)

SQL: ALTER TABLE `tutorials` ADD CONSTRAINT tutorials_author_foreign FOREIGN KEY (`author`) REFERENCES `users` (`id`)

Bindings: array (
)

Based on posts around the web and the limited documentation available on how to setup Laravel’s Eloquent relationships, I’m not sure what I’m doing wrong…

users already exists and it does have an id field that is auto_increment. I am also setting up my models with the proper relationships (belongs_to and has_many), but as far as I can tell this is not the issue– it’s the database setup. The DB is InnoDB.

What exactly am I doing wrong with the foreign key?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:55:10+00:00Added an answer on June 9, 2026 at 1:55 pm

    I’m not 100% sure if these are the reasons this is failing but a couple of pointers. If you’re using an older version of mySQL as the database, the default table implementation is myISAM that does not support foreign key restraints. As your scripts are failing on the foreign key assignment, you are better off explicitly stating that you want INNODB as the engine using this syntax in Schema’s create method.

    Schema::create('lessons', function($table)
    {
        $table->engine = 'InnoDB';
    
        $table->increments('id');
        $table->string('title')->nullable();
        $table->string('summary')->nullable();
        $table->timestamps();
    });
    

    This should hopefully alleviate the problems you are having.

    Also, whilst you can declare foreign keys as an afterthought, I create the foreign keys within the initial schema as I can do an easy check to make sure I’ve got the right DB engine set.

    Schema::create('tutorials', function($table)
    {
        $table->engine = 'InnoDB';
    
        $table->increments('id');
        $table->integer('author');
        $table->integer('lesson');
        $table->string('title')->nullable();
        $table->string('summary')->nullable();
        $table->string('tagline')->nullable();
        $table->text('content')->nullable();
        $table->text('attachments')->nullable();
        $table->timestamps();
    
        $table->foreign('author')->references('id')->on('users');
        $table->foreign('lesson')->references('id')->on('lessons');
    });
    

    Hope this helps / solves your problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm essentially having difficulty pass dynamic variables to a view. I have php functions
This is my first post to I apoligise if its not perfectly put. Essentially
first time post for me :) I'm having this issue with the width of
Essentially, after compiling utility jars w/ ANT, I'm having the problem that all the
Essentially, is there any way to make this code legal? main = print .
Essentially, I have a binary voting system Like/Dislike. Thee class is called Like It
Essentially I have a method of a class called killProgram, which is intended to
I have been running into some issues with animating multiple CALayers at the same
I’m having fun with EF and have come unstuck. Originally I used the following
I'm having a bit of a hard time. Essentially I need a data structure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.