基本上,我和这个家伙有同样的问题,减go 表前缀.因为我没有表前缀,所以他的修正不起作用.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已经存在,它does有一个id字段,即auto_increment.我也在用正确的关系(belongs_tohas_many)建立模型,但据我所知,这不是问题所在——这是数据库设置.DB is InnoDB.

What exactly am I doing wrong with the foreign key?

推荐答案

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.

此外,虽然您可以事后声明外键,但我在初始模式中创建外键,因为我可以进行简单的判断,以确保设置了正确的DB引擎.

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.

Laravel相关问答推荐

Laravel Eloquent,仅 Select 存在关系的行

Laravel 5.1 - 如何在进度条上设置消息

Laravel 5.4 中的自定义助手类

Laravel 控制器中一种特定方法的中间件

laravel 控制器中的全局变量

Laravel:Blade foreach 循环 bootstrap 列

laravel url 验证变音符号

在集合 laravel 中使用查询范围

Laravel lockforupdate(悲观锁)

使用数据库用户提供程序时如何在 Laravel 中创建密码重置方法

在 Laravel 4 迁移中创建 MYSQL 过程

laravel 4:更新行时如何从列的当前值中减一?

Laravel Eloquent - 加入表时防止覆盖值

是否可以将路由参数传递给 Laravel 中的控制器构造函数?

无法声明类 Controller,因为该名称已在使用中

Mac OS X 需要 Mcrypt PHP 扩展

laravel如何访问具有表编号名称的列?

如何判断 Laravel 集合是否为空?

为什么`catch (Exception $e)` 不处理这个`ErrorException`?

如何在 laravel 表单验证错误消息中给出自定义字段名称