有人可以帮我吗?我是Laravel的新手,当我尝试做时:
gcc -shared -fPIC -I/home/Data/libsodium/include \
-L/home/Data/libsodium/lib -Wl,-rpath=/home/Data/libsodium/lib \
-o vrf.so vrf.c -lsodium
我得到了错误:
Illuminate \ Database \ QueryException:SQLSTATE [HY000]:常规错误:1215无法添加外键约束(SQL:alter table posts添加约束posts_author_id_foreign外键(author_id)在删除限制上引用了用户(id))
这是帖子表的内容:
php artisan migrate
我注意到当我评论这一行时:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('author_id')->unsigned();
$table->foreign('author_id')->references('id')->on('users')->onDelete('restrict');
$table->string('title');
$table->string('slug')->unique();
$table->text('excerpt');
$table->text('body');
$table->string('image')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
错误消失了,所以我想问题出在那一行。那么,有人可以告诉我什么是正确的语法或如何解决它吗?
答案 0 :(得分:0)
两个表上的列数据类型必须相同,外键才能起作用。默认情况下,用户ID列为:
$table->bigIncrements('id');
因此,您需要使用bigInteger
进行posts
迁移:
$table->bigInteger('author_id')->unsigned();
答案 1 :(得分:0)
我做了更改。这部分:
$table->bigIncrements('id');
$table->bigInteger('author_id')->unsigned();
$table->foreign('author_id')->references('id')->on('users')->onDelete('restrict');
$table->string('title');
$table->string('slug')->unique();
$table->text('excerpt');
$table->text('body');
$table->string('image')->nullable();
$table->timestamps();
但是我再次遇到相同的错误,这是完整的错误:
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `posts` add constraint `posts_author_id_foreign` foreign key (`author_id`) references `users` (`id`) on delete restrict)
at /var/www/html/becauseican/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
Exception trace:
1 PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")
/var/www/html/becauseican/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458
2 PDOStatement::execute()
/var/www/html/becauseican/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458
p.s。我还添加了这一行:
$table->engine = 'InnoDB';
但没有任何改变
答案 2 :(得分:0)
尝试将外来ko更改为unsignedInteger
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('author_id');
$table->foreign('author_id')->references('id')->on('users')->onDelete('restrict');
$table->string('title');
$table->string('slug')->unique();
$table->text('excerpt');
$table->text('body');
$table->string('image')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}