嗨,我正在尝试在我的数据库中创建名为"品牌"和"产品"的2个表.我为"品牌"创建了名为"create_brand"的迁移文件,其中包含:
public function up()
{
Schema::create('brand', function($table){
$table->increments('brand_id');
$table->string('brand_name', 100);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('brand');
}
Run Code Online (Sandbox Code Playgroud)
名为"create_product"的"product"的迁移文件包含:
public function up()
{
Schema::create('product', function($table){
$table->increments('skuid');
$table->integer('brand_id')->unasigned();
});
Schema::create('product', function($table){
$table->foreign('brand_id')->references('brand_id')->on('brand')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('product', function($table){
$table->dropForeign('brand_id');
});
Schema::drop('product');
}
Run Code Online (Sandbox Code Playgroud)
现在,当我运行"php artisan migrate"时,我收到错误:
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error i
your SQL syntax; check the manual that corresponds to your MySQL server v
ersion for the right syntax to use near ') default character set utf8 colla
te utf8_unicode_ci' at line 1 (SQL: create table `product` () default chara
cter set utf8 collate utf8_unicode_ci)
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error i
n your SQL syntax; check the manual that corresponds to your MySQL server v
ersion for the right syntax to use near ') default character set utf8 colla
te utf8_unicode_ci' at line 1
Run Code Online (Sandbox Code Playgroud)
如果有人能帮我解决这个问题,我将非常感激:)谢谢.
小智 7
Schema::create只使用一次.编辑表使用Schema::table
当使用外键时,类型应该是unsignedInteger.如果您创建一个基本整数并尝试在其上放置一个外键,它将失败.
将您的密码更改为
public function up()
{
Schema::create('product', function($table){
$table->increments('skuid');
$table->unsignedInteger('brand_id');
});
Schema::table('product', function($table){
$table->foreign('brand_id')->references('brand_id')->on('brand');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('product');
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5848 次 |
| 最近记录: |