Skip to content

[BUG] SQLite foreign keys are failing in migrations #7341

@binaryfire

Description

@binaryfire

SQLite foreign keys are failing in migrations.

To reproduce:

  1. Clone https://github.com/binaryfire/hyperf-test
  2. Run ./start.sh
  3. You will see the error during the migration process

config/autoload/databases.php:

return [
    'default' => [
        'driver' => 'sqlite',
        'host' => 'localhost',
        'database' => base_path('database/'. env('DB_DATABASE', 'database.sqlite')),
        'charset' => env('DB_CHARSET','utf8'),
        'collation' => env('DB_COLLATION','utf8_unicode_ci'),
        'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
    ],
];

Error occurs when the 2025_04_07_023022_create_tasks_table migration (which contains a foreign key constraint) runs:

Migrating: 2020_08_03_064816_telescope_entries
Migrated:  2020_08_03_064816_telescope_entries
Migrating: 2025_04_07_023021_create_workflows_table
Migrated:  2025_04_07_023021_create_workflows_table
Migrating: 2025_04_07_023022_create_tasks_table

In Connection.php line 375:

  PDO::prepare(): Argument #1 ($query) cannot be empty


migrate:fresh [--disable-event-dispatcher] [--database [DATABASE]] [--drop-views] [--force] [--path [PATH]] [--realpath] [--seed] [--seeder [SEEDER]] [--step]

[ERROR] ValueError: PDO::prepare(): Argument #1 ($query) cannot be empty in /home/binaryfire/workspace/tmp/hyperf-test/vendor/hyperf/database/src/Connection.php:375
Stack trace:
#0 /home/binaryfire/workspace/tmp/hyperf-test/vendor/hyperf/database/src/Connection.php(375): PDO->prepare()
#1 /home/binaryfire/workspace/tmp/hyperf-test/vendor/hyperf/database/src/Connection.php(1177): Hyperf\Database\Connection->Hyperf\Database\{closure}()
#2 /home/binaryfire/workspace/tmp/hyperf-test/vendor/hyperf/database/src/Connection.php(1142): Hyperf\Database\Connection->runQueryCallback()
#3 /home/binaryfire/workspace/tmp/hyperf-test/vendor/hyperf/database/src/Connection.php(370): Hyperf\Database\Connection->run()
#4 /home/binaryfire/workspace/tmp/hyperf-test/vendor/hyperf/database/src/Schema/Blueprint.php(119): Hyperf\Database\Connection->statement()
#5 /home/binaryfire/workspace/tmp/hyperf-test/vendor/hyperf/database/src/Schema/Builder.php(403): Hyperf\Database\Schema\Blueprint->build()
#6 /home/binaryfire/workspace/tmp/hyperf-test/vendor/hyperf/database/src/Schema/Builder.php(272): Hyperf\Database\Schema\Builder->build()
#7 /home/binaryfire/workspace/tmp/hyperf-test/vendor/hyperf/database/src/Schema/Schema.php(59): Hyperf\Database\Schema\Builder->create()
#8 /home/binaryfire/workspace/tmp/hyperf-test/migrations/2025_04_07_023022_create_tasks_table.php(13): Hyperf\Database\Schema\Schema::__callStatic()
#9 /home/binaryfire/workspace/tmp/hyperf-test/vendor/hyperf/database/src/Migrations/Migrator.php(462): Hyperf\Database\Migrations\Migration@anonymous->up()
#10 /home/binaryfire/workspace/tmp/hyperf-test/vendor/hyperf/database/src/Migrations/Migrator.php(471): Hyperf\Database\Migrations\Migrator->Hyperf\Database\Migrations\{closure}()
#11 /home/binaryfire/workspace/tmp/hyperf-test/vendor/hyperf/database/src/Migrations/Migrator.php(339): Hyperf\Database\Migrations\Migrator->runMigration()
#12 /home/binaryfire/workspace/tmp/hyperf-test/vendor/hyperf/database/src/Migrations/Migrator.php(115): Hyperf\Database\Migrations\Migrator->runUp()
#13 /home/binaryfire/workspace/tmp/hyperf-test/vendor/hyperf/database/src/Migrations/Migrator.php(81): Hyperf\Database\Migrations\Migrator->runPending()
#14 /home/binaryfire/workspace/tmp/hyperf-test/vendor/hyperf/database/src/Commands/Migrations/MigrateCommand.php(68): Hyperf\Database\Migrations\Migrator->run()
#15 /home/binaryfire/workspace/tmp/hyperf-test/vendor/hyperf/database/src/Commands/Migrations/MigrateCommand.php(43): Hyperf\Database\Commands\Migrations\MigrateCommand->runMigrations()
#16 /home/binaryfire/workspace/tmp/hyperf-test/vendor/hyperf/command/src/Command.php(177): Hyperf\Database\Commands\Migrations\MigrateCommand->handle()
#17 /home/binaryfire/workspace/tmp/hyperf-test/vendor/hyperf/command/src/Command.php(206): Hyperf\Command\Command->Hyperf\Command\{closure}()
#18 /home/binaryfire/workspace/tmp/hyperf-test/vendor/symfony/console/Command/Command.php(279): Hyperf\Command\Command->execute()
#19 /home/binaryfire/workspace/tmp/hyperf-test/vendor/hyperf/command/src/Command.php(107): Symfony\Component\Console\Command\Command->run()
#20 /home/binaryfire/workspace/tmp/hyperf-test/vendor/hyperf/command/src/Command.php(117): Hyperf\Command\Command->run()
#21 /home/binaryfire/workspace/tmp/hyperf-test/vendor/hyperf/database/src/Commands/Migrations/FreshCommand.php(52): Hyperf\Command\Command->call()
#22 /home/binaryfire/workspace/tmp/hyperf-test/vendor/hyperf/command/src/Command.php(177): Hyperf\Database\Commands\Migrations\FreshCommand->handle()
#23 [internal function]: Hyperf\Command\Command->Hyperf\Command\{closure}()
#24 {main}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions