Laravel迁移似乎只为sqlite生成错误的语句

kmbjn2e3  于 2023-05-07  发布在  SQLite
关注(0)|答案(2)|浏览(205)

我在laravel项目中有一个迁移,我重命名了字段。这个迁移在mysql和我测试过的大多数数据库驱动程序(还有我本地的sqlite)上都运行得很好,不管是什么原因,github ci runner的语法似乎有问题,我相信要么是版本不匹配,要么是laravel生成了一个错误的sql语句。这是我的迁徙:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('user_weights', function (Blueprint $table) {
            $table->renameColumn('weight', 'weight_raw');
        });

        Schema::table('user_heights', function (Blueprint $table) {
            $table->renameColumn('height', 'height_raw');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('user_weights', function (Blueprint $table) {
            $table->renameColumn('weight_raw', 'weight');
        });

        Schema::table('user_heights', function (Blueprint $table) {
            $table->renameColumn('height_raw', 'height');
        });
    }
};

我的github工作流程看起来像这样:

on:
  push:
    branches:
      - master
      - feature/*
      - hotfix/*
  pull_request:
    branches:
      - master

name: CI
jobs:
  phpunit:
    runs-on: ubuntu-latest
    container:
      image: kirschbaumdevelopment/laravel-test-runner:8.1

    steps:
      - uses: actions/checkout@v1
        with:
          fetch-depth: 1

      - name: Install composer dependencies
        run: |
          composer config "http-basic.nova.laravel.com" "${{ secrets.NOVA_USERNAME }}" "${{ secrets.NOVA_LICENSE }}"
          composer install --no-scripts

      - name: Prepare .env
        run: cp .env.testing .env

      - name: Generate key
        run: php artisan key:generate

      - name: Directory Permissions
        run: chmod -R 777 storage bootstrap/cache

      - name: Mark app folder as safe directory
        run: git config --global --add safe.directory /__w/fitness-backend-neu/fitness-backend-neu

      - name: Create Database
        run: |
          mkdir -p database
          touch database/database.sqlite
          php artisan migrate

      - name: Run Testsuite
        run: vendor/bin/phpunit tests/

      - name: Run Pint
        run: vendor/bin/pint --test

工作流在数据库迁移中崩溃,并出现以下错误:

In Connection.php line 759:
                                                                               
  SQLSTATE[HY000]: General error: 1 near "0": syntax error (SQL: CREATE TABLE  
   user_weights (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, user_id VARCH  
  AR(255) NOT NULL COLLATE BINARY, weight_raw DOUBLE PRECISION NOT NULL, date  
   DATE NOT NULL, created_at DATETIME DEFAULT NULL, updated_at DATETIME DEFAU  
  LT NULL, CONSTRAINT 0 FOREIGN KEY (user_id) REFERENCES users (id) ON UPDATE  
   NO ACTION ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE))            
                                                                               

In Connection.php line 538:
                                                            
  SQLSTATE[HY000]: General error: 1 near "0": syntax error

由于错误发生在“user_id”关系中,我已经尝试在重命名之前删除该关系,并在重命名过程之后立即重新创建该关系。由于sqlite不支持删除外键约束,所以这不起作用。
我还尝试使用act重现这个错误。Act是一个本地ci运行器,它也解释GitHub工作流文件。使用act运行的完全相同的设置,我仍然无法重现此错误。除了GitHub Actions之外,一切都正常(Act,PHPUnit,本地环境迁移,生产环境迁移)。
我刚刚看到,之前通过GitHub操作运行了完全相同的迁移,并且没有失败。它只是在一些其他提交后开始失败。

a2mppw5e

a2mppw5e1#

我找到了一个解决方法,因为在这种情况下laravel似乎会生成一个错误的sql语句。我在迁移中添加了以下代码,以捕获sqlite连接上的sql语句并运行“仅sqlite”查询。

if (config('database.default') === 'sqlite') {
            DB::statement('ALTER TABLE user_weights RENAME COLUMN weight TO weight_raw;');
            DB::statement('ALTER TABLE user_heights RENAME COLUMN height TO height_raw;');

            return;
        }

我也做了同样的反向迁移。

unftdfkk

unftdfkk2#

我在"doctrine/dbal": "^3.1"上也遇到了同样的问题,但是更新doctrine/dbal解决了这个问题。

相关问题