SQLSTATE[HY000]:一般错误:无法创建表

Posted

技术标签:

【中文标题】SQLSTATE[HY000]:一般错误:无法创建表【英文标题】:SQLSTATE[HY000]: General error:Can't create table 【发布时间】:2021-06-16 15:27:31 【问题描述】:

我在 CentOS 8 服务器上使用 Laravel 8.21.0。我正在使用mariaDB。我有 3 个表:测试、学生和成绩。 我正在尝试在成绩表上设置测试和学生的外键。 但是,当我运行迁移时,我得到错误号 150:外键约束的格式不正确。

这是我的迁移:

成绩表:

class CreateGradesTable extends Migration

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    
        Schema::create('grades', function (Blueprint $table) 
            
           $table->id('id')->unique();
           $table->unsignedInteger('student_id');
           $table->string('test_id');

           $table->foreign('test_id')->references('id')->on('tests');
           $table->foreign('student_id')->references('id')->on('students');

           $table->date('testDate');
           $table->integer('testCount');
           $table->integer('vocabScore');
           $table->integer('readingScore');
           $table->integer('listeningScore');
           $table->integer('rawTotal');
           $table->integer('adjustVocabScore');
           $table->integer('adjustReadingScore');
           $table->integer('adjustlisteningScore');
           $table->integer('adjustTotal');
           $table->string('passOrfail');
           $table->timestamps();
            
        );
    

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    
        Schema::dropIfExists('grades');
    

学生表迁移:

class CreateStudentsTable extends Migration

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    
        Schema::create('students', function (Blueprint $table) 
          
            $table->integer('id')->unique();
            $table->string('name');
            $table->date('DOE');
            $table->string('belongsTo');
            $table->string('country');
            $table->string('level');
            $table->string('year');
            $table->timestamps();
            
        );
    

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    
        Schema::dropIfExists('students');
    

测试表:

class CreateTestsTable extends Migration

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    
        Schema::create('tests', function (Blueprint $table) 
         
            $table->string('id')->unique();
            $table->string('testName');
            $table->string('level');
            $table->integer('vocabFullScore');
            $table->integer('readingFullScore');
            $table->integer('listeningFullScore');
            $table->integer('totalFullScore');
            $table->integer('vocabPassScore');
            $table->integer('readingPassScore');
            $table->integer('listeningPassScore');
            $table->integer('totalPassScore');
            $table->timestamps();
            
        );
    

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    
        Schema::dropIfExists('tests');
    

奇怪的是,当我在 localhost WAMP 服务器上运行迁移表时,它成功创建了它,但它在 CENTOS 服务器上抛出了一个错误。有人知道如何解决这个问题吗?

我尝试过但没有成功的事情:

・Changed database to InnoDB  by specifying 'engine' => 'InnoDB' on each model.
・Made sure that the order of migration is correct. First migrated student table, then tests and lastly grades.
・Ensure that the data type of the foreign key is correct on grades table. 

任何想法将不胜感激。 :'(

编辑:我将 student_id 的外键类型设置为整数。 在成绩表迁移中:

$table->integer('student_id');
$table->foreign('student_id')->references('id')->on('students');

在学生表迁移中:

$table->integer('id')->unique();

执行此操作后,我收到一个新错误:

Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for right syntax to use near ')' at line 1 (SQL: alter table 'grades' add constraint 'grades_test_id_foreign' foreign key ('test_id') references 'tests' ()) 

at vendor/laravel/framework/src/Illuminate/Database/Connection.php:678
catch(Exception $e)
 throw new QueryException(
  $query, $this->prepareBindings($bindings),$e
);

+9 vendor frames
database/migrations/2021_01_13_064711_create_grades_table.php:54
Illuminate\Support\Facades\Facade::__callStatic()

+32 vendor frames
artisan:37
Illuminate\Foundation\Console\Kernel::handle()

【问题讨论】:

将 $table->unsignedInteger('student_id') 的数据类型更改为 int,因为两个表中的 ID 应该是相同的类型 在成绩表中将 $table->unsignedInteger('student_id'); 更改为 $table->integer('student_id'); 或在学生表中设置 $table->unsignedInteger('id'); 感谢您的回复。我在成绩表上尝试了 $table->integer('student_id') 。现在,当我运行迁移时出现新错误。语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 1 行的 ')' 附近使用正确的语法(SQL:alter table 'grades' 添加约束 'grades_test_id_foreign' 外键 ('test_id') 引用 'tests' ()) @Pradeep 我根据您的建议更改了迁移并收到一个新错误。你能帮帮我吗? 【参考方案1】:

改变

$table->unsignedInteger('student_id');

$table->integer('student_id');

grades表中匹配数据类型形成约束。

【讨论】:

students 表虽然没有使用 ->id()(如果这就是你的意思 $table('id')),它使用 $table->integer('id')->unique(); 更新答案。 感谢您的回复。我在成绩表上尝试了 $table->integer('student_id') 。现在,当我运行迁移时出现新错误。语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 1 行的 ')' 附近使用正确的语法(SQL:alter table 'grades' add constraint 'grades_test_id_foreign' 外键 ('test_id') 引用 'tests' ())– 编辑你的答案并在那里添加完整的错误。 这是我得到的错误:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 1 行的 ')' 附近使用正确的语法(SQL:alter table 'grades' add constraint 'grades_test_id_foreign' 外键 ('test_id') 引用 'tests' ())供应商/laravel/framework/src/Illuminate/Database/Connection.php:678 catch(Exception $e) throw new QueryException($query, $this->prepareBindings($bindings),$e);

以上是关于SQLSTATE[HY000]:一般错误:无法创建表的主要内容,如果未能解决你的问题,请参考以下文章

SQLSTATE [HY000]:一般错误:1005 无法创建表`Data`.`company eligibilities`(errno:150“外键约束形成错误”)

SQLSTATE [HY000]:一般错误:1005无法创建表`Projectname`.`users`(errno:150“外键约束格式不正确”)[重复]

SQLSTATE[HY000]:一般错误:1215 无法添加外键约束 Laravel 5.8

SQLSTATE [HY000]:一般错误:1005 无法创建表 `ic`.`livros`(errno: 150 "外键约束格式不正确") id`))

Laravel 5.0 [PDOException] SQLSTATE[HY000]:一般错误:1215 无法添加外键约束

SQLSTATE[HY000]:一般错误:1215 无法添加外键约束 [Laravel 7.0]