'symfony console make:migration' 不创建 SQL 代码

'symfony console make:migration' doesn't create SQL code

我遵循了快速通道一书的“8.7 Migrer la base de données”一章。

我的 2 个实体 类 通过 symfony console make:entity 正确生成:

ls -1 src/Entity/Co*
src/Entity/Comment.php
src/Entity/Conference.php

https://pastebin.com/6RJmQTEg
https://pastebin.com/XwZ7csS3

运行symfony console make:migration,只得到:


<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
 * Auto-generated Migration: Please modify to your needs!
 */
final class Version20200322201522 extends AbstractMigration
{
    public function getDescription() : string
    {
        return '';
    }

    public function up(Schema $schema) : void
    {
        // this up() migration is auto-generated, please modify it to your needs

    }

    public function down(Schema $schema) : void
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');

        $this->addSql('CREATE SCHEMA public');
    }
}

为什么没有 SQL 代码生成?

我的.env:

APP_ENV=dev
APP_SECRET=xxx
DATABASE_URL=postgresql://127.0.0.1:5432/db?serverVersion=11&charset=utf8`

命令symfony run psql运行很好;我得到 psql 提示:

[mevatlave:~/sources/guestbook] master* ± symfony run psql
psql (11.7 (Debian 11.7-0+deb10u1))
Type "help" for help.

main=# 
symfony -V
Symfony CLI version v4.13.3 (Thu Mar 19 15:24:08 UTC 2020)
php bin/console debug:container --env-vars

Symfony Container Environment Variables
=======================================

 -------------- --------------- ---------------------------------------------------------------- 
  Name           Default value   Real value                                                      
 -------------- --------------- ---------------------------------------------------------------- 
  APP_SECRET     n/a             "4ec3efda62f1b3001e78459335893c3f"                              
  DATABASE_URL   n/a             "postgresql://127.0.0.1:5432/db?serverVersion=11&charset=utf8"  
 -------------- --------------- ---------------------------------------------------------------- 

 // Note real values might be different between web and CLI. 

从未编辑过 Symfony 配置:

cat config/packages/doctrine.yaml
doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'

        # IMPORTANT: You MUST configure your server version,
        # either here or in the DATABASE_URL env var (see .env file)
        #server_version: '5.7'
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App

先尝试清除缓存。

否则试试这个: 你需要 doctrine:migrations:diff。这将生成一个新的迁移,该迁移预填充了您将从 doctrine:schema:update

中获得的 SQL

所以:

bin/console doctrine:migrations:diff
OR
bin/console d:m:d

然后你去检查新创建的文件,如果一切正常你执行它:

// (to be 100% sure take a backup of your database before doing:)

bin/console doctrine:migrations:migrate
OR
bin/console d:m:m

如果你因为犯了错误或其他原因想要返回

// Replace 20190215095613 with your migration number!
bin/console doctrine:migrations:execute --down 20190215095613
  • 然后可以修改迁移,再次执行"bin/console d:m:m"
  • 或者放弃迁移并从 "bin/console d:m:d"
  • 重新开始

OK,发现书里有个bug,DSN错了:

DATABASE_URL=postgresql://127.0.0.1:5432/db?serverVersion=11&charset=utf8

现在:DATABASE_URL=postgresql://main:main@127.0.0.1:5432/db?serverVersion=11&charset=utf8

缺少username/password。现在 symfony console make:migration 运行良好