“一般错误:1005不能创建表”使用Laravel架构构建和外键 [英] "General error: 1005 Can't create table" Using Laravel Schema Build and Foreign Keys

查看:194
本文介绍了“一般错误:1005不能创建表”使用Laravel架构构建和外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我和这个人有同样的问题,减去表前缀。因为我没有表前缀,他的修复不起作用。 http://forums.laravel.com/viewtopic.php?id=972



我试图使用Laravel的架构生成器来构建一个表格:

pre $ ($'$'$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' (');> nullable();
$ table-> string('summary') - > nullable();
$ table-> timestamps();
} ); (b)












$ b $ > 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');
} );

问题是,当我运行这个代码时(在/ setup路径中),我得到以下错误:
$ b $

 SQLSTATE [HY000]:常规错误:1005无法创建表'tutorials。#sql-2cff_da'(errno :150)

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

绑定:array(





$ b

基于网络上的文章和有限的文档,设置 Laravel的雄辩关系,我不知道我做错了什么...



用户已经存在,而且 id 字段是 auto_increment 。我也建立了适当的关系( belongs_to has_many )的模型,但据我所知这不是问题 - 这是数据库设置。 DB $ em是InnoDB。

我在做什么错误的外键?

解决方案

我不是100%确定,如果这些是失败的原因,但几个指针。如果您使用的是旧版本的mySQL作为数据库,则缺省的表实现是myISAM,它不支持外键约束。由于您的脚本在外键分配上失败,最好在Schema的create方法中明确声明您需要使用INNODB作为引擎。

  Schema :: create('lessons',function($ table)
{
$ table-> engine ='InnoDB';

$ table - >增量('id');
$ table-> string('title') - > nullable();
$ table-> string('summary') - > nullable();
$ table-> timestamps();
});

这应该有望缓解您遇到的问题。



另外,虽然你可以将外键声明为事后处理,但是我可以在初始模式中创建外键,因为我可以做一个简单的检查,以确保我已经设置了正确的数据库引擎。 b
$ b $ $ p $ code $ Schema :: create('tutorials',function($ table)
{
$ table-> engine =' InnoDB';

$ table->增量('id');
$表 - >整数('author');
$表 - >整数');
$ 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') - > ('id') - > on('users');
$ table-> fo元年(教训) - > - ;在(教训);>引用( ID)
});

希望这可以帮助您/解决您的问题。


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?

解决方案

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.

这篇关于“一般错误:1005不能创建表”使用Laravel架构构建和外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆