我正在从事一个项目,我正在使用laravel 5.8。这里我想创建一个外键。我试了很多次,但每次都收到一条关于迁移时间的错误消息。有三个表一个是行业,是小学和学生,这些是第二个表。我想在这两个辅助表trade_id上创建一个外键作为键的名称。
这是交易表(主表)。
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTradesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('trades', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('trade', 32)->nullable();
$table->string('unit', 32)->nullable();
$table->string('class_room', 32)->nullable();
$table->string('total_class_room', 32)->nullable();
$table->string('workshop', 32)->nullable();
$table->string('total_workshops', 32)->nullable();
$table->string('remarks', 32)->nullable();
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('trades');
}
}
这是学生表(辅助表)。
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateStudentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('students', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name', 32)->nullable();
$table->string('father_name', 32)->nullable();
$table->string('uid', 12)->nullable();
$table->date('dob');
$table->string('sex', 6)->nullable();
$table->string('status', 16)->nullable();
$table->integer('trade_id')->unsigned()->nullable();
$table->foreign('trade_id')->references('id')->on('trades')->onDelete('cascade');
$table->string('shift', 32)->nullable();
$table->string('session', 32)->nullable();
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('students');
}
}
这是放置表(辅助表)。
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePlacementsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('placements', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('roll_no', 32)->nullable();
$table->string('name', 32)->nullable();
$table->integer('trade_id')->unsigned()->nullable();
$table->foreign('trade_id')->references('id')->on('trades')->onDelete('cascade');
$table->year('year_of_passing');
$table->string('organization_name', 32)->nullable();
$table->float('salary_on_joining', 8,2);
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('placements');
}
}
在迁移时出现错误。
$ php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table
Migrating: 2019_05_22_132427_create_notices_table
Migrated: 2019_05_22_132427_create_notices_table
Migrating: 2019_05_22_132600_create_students_table
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `students` add co
nstraint `students_trade_id_foreign` foreign key (`trade_id`) references `trades` (`id`) on delete cascade)
at C:\laragon\www\projects\UIIT-admin\uiti_admin\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")
C:\laragon\www\projects\UIIT-admin\uiti_admin\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
2 PDOStatement::execute()
C:\laragon\www\projects\UIIT-admin\uiti_admin\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
Please use the argument -v to see more details.
按照以下步骤更改外键列代码
//In students
Schema::create('students', function (Blueprint $table) {
$table->unsignedBigInteger('trade_id')->nullable();
$table->foreign('trade_id')->references('id')->on('trades')->onDelete('cascade');
});
//In placements
Schema::create('placements', function (Blueprint $table) {
$table->unsignedBigInteger('trade_id')->nullable();;
$table->foreign('trade_id')->references('id')->on('trades')->onDelete('cascade');
});
您已将外键定义为整数类型,但交易表中的primery键是bigintger类型
试试这个吧
$table->bigInteger('trade_id')->unsigned()->nullable();
$table->foreign('trade_id')->references('id')->on('trades')->onDelete('cascade');
我有一个问题与我的Laravel迁移:( 当我运行php artisan migrate时,它会在外键上停止。 首次迁移 还有第二个 运行命令后,我出现以下错误: [Illumb\Database\QueryException] SQLSTATE[HY000]:一般错误:1005无法创建表gsb_larave.35; sql-176_b9(错误号:150“外键约束格式不正确”)(sql:alter
我想在用户表中添加一个外键: 一个用户有一个城镇(法语为ville),一个城镇可以有多个用户: 我的村庄(城镇)迁移 我重命名了迁移(我不知道这样做是否有好处?),这是我迁移的顺序,正如您所看到的,我在用户之前创建了villes: 我在终端中发现了这个错误: 照亮\数据库\查询异常 SQLSTATE[42000]:语法错误或访问冲突:1072键列“villes\u id”在表中不存在(SQL:al
我已经发现了几十个类似的问题,但没有一个解决方案在我的案例中有效。当我运行“php artisan migrate:fresh”时,它抛出了错误。。。 illumb\Database\QueryException:SQLSTATE[HY000]:一般错误:1215无法添加外键约束(SQL:alter tableadd constraint外键()引用()关于删除级联) > 我检查auth用户表,它
我试图在一次迁移时在主键中做一个外键。 这是为Laravel 5.7。我尝试了不同的方法来实现我的目标。这是我的最终代码。 它生成一个普通表,其中“user_id”作为主键,但它不是外键。
问题内容: 有没有办法在Laravel 4迁移中生成存储的MYSQL过程? 例如,这是一个简单的过程生成查询,以字符串形式存储(通过Heredoc) 在迁移函数中运行此命令时,出现以下错误: 问题答案: 您的代码有两个主要问题 不是有效的sql语句。这只是一个MySql客户端命令。因此,请不要使用它。 顺便说一句 ,您得到的错误恰恰告诉您。 您无法使用来执行代码,因为它使用的预准备语句源代码。您可
我试图在Laravel中创建外键,但是当我使用artisan迁移我的表时,我被抛出以下错误: 模块迁移表: 课程迁移表: 任何关于我做错了什么的想法,我想现在就得到这个,因为我需要创建很多表,例如用户、客户端、项目、任务、状态、优先级、类型、团队。理想情况下,我想创建包含外键、i... eclients_project和project_tasks等数据的表。 希望有人能帮助我开始。