>source

통합 테스트 목적으로 테스트 환경에 대한 sqlite DB를 만들려고하지만 php bin/console doctrine:schema:create --env=test 를 실행할 때  테이블이 이미 존재한다는 오류가 발생합니다. 실제로 새로운 테스트 DB를 생성하지 않고 기존 DB를 계속 진행하기 때문에 가정합니다.

env.test에서 읽지 않는 것 같습니다.

나는 doctrine.yaml 를 만들었습니다  config/test에서 다음과 같이하십시오 :

doctrine:
dbal:
    driver: 'pdo_sqlite'
    url: 'sqlite:///%kernel.project_dir%/var/data/test.sqlite'

sqlite 테스트 db를 만들기 위해 누락 된 것은 무엇입니까?

오류가 발생하는 이상한 일 :

In PDOConnection.php line 90:
 SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'user_menu' already exists

왜 SQL인가? sqlite의 IT가 아니어야합니까, 맞습니까?

config/packages/doctrine.yaml에는 다음이 있습니다 :

parameters:
# Adds a fallback DATABASE_URL if the env var is not set.
# This allows you to run cache:warmup even if your
# environment variables are not available yet.
# You should not need to change this value.
env(DATABASE_URL): ''
doctrine:
dbal:
    # configure these for your database server
    connections:
            gui:
                driver: 'pdo_mysql'
                server_version: '5.7'
                charset: utf8mb4
                default_table_options:
                    charset: utf8mb4
                    collate: utf8mb4_unicode_ci
                url: '%env(resolve:DATABASE_GUI_URL)%'
            upv6:
                driver: 'pdo_mysql'
                server_version: '5.7'
                charset: utf8mb4
                default_table_options:
                    charset: utf8mb4
                    collate: utf8mb4_unicode_ci
                url: '%env(resolve:DATABASE_UPV6_URL)%'
orm:
    auto_generate_proxy_classes:  true # <- change to true
    proxy_dir:            '%kernel.cache_dir%/doctrine/orm/Proxies'
    proxy_namespace:      Proxies
    entity_managers:
        gui:
            connection: gui
            mappings:
                Gui:
                    is_bundle: false
                    type: annotation
                    dir: '%kernel.project_dir%/src/Entity/Gui'
                    prefix: 'App\Entity\Gui'
                    alias: Gui
        upv6:
            connection: upv6
            mappings:
                Upv6:
                    is_bundle: false
                    type: annotation
                    dir: '%kernel.project_dir%/src/Entity/Upv6'
                    prefix: 'App\Entity\Upv6'
                    alias: Upv6

내 교리 설정 :

doctrine:
dbal:
    connections:
        gui:
            driver: pdo_mysql
            server_version: '5.7'
            charset: utf8mb4
            default_table_options:
                charset: utf8mb4
                collate: utf8mb4_unicode_ci
            url: '%env(resolve:DATABASE_GUI_URL)%'
            host: localhost
            port: null
            user: root
            password: null
            logging: true
            profiling: true
            options: {  }
            mapping_types: {  }
            slaves: {  }
            shards: {  }
        upv6:
            driver: pdo_mysql
            server_version: '5.7'
            charset: utf8mb4
            default_table_options:
                charset: utf8mb4
                collate: utf8mb4_unicode_ci
            url: '%env(resolve:DATABASE_UPV6_URL)%'
            host: localhost
            port: null
            user: root
            password: null
            logging: true
            profiling: true
            options: {  }
            mapping_types: {  }
            slaves: {  }
            shards: {  }
        default:
            driver: pdo_sqlite
            url: '%env(resolve:DATABASE_URL)%'
            host: localhost
            port: null
            user: root
            password: null
            logging: true
            profiling: true
            options: {  }
            mapping_types: {  }
            default_table_options: {  }
            slaves: {  }
            shards: {  }
    default_connection: default
    types: {  }
orm:
    auto_generate_proxy_classes: true
    proxy_dir: /var/www/symfony/var/cache/test/doctrine/orm/Proxies
    proxy_namespace: Proxies
    entity_managers:
        gui:
            connection: gui
            mappings:
                Gui:
                    is_bundle: false
                    type: annotation
                    dir: /var/www/symfony/src/Entity/Gui
                    prefix: App\Entity\Gui
                    alias: Gui
                    mapping: true
            query_cache_driver:
                type: array
                namespace: null
                cache_provider: null
            metadata_cache_driver:
                type: array
                namespace: null
                cache_provider: null
            result_cache_driver:
                type: array
                namespace: null
                cache_provider: null
            class_metadata_factory_name: Doctrine\ORM\Mapping\ClassMetadataFactory
            default_repository_class: Doctrine\ORM\EntityRepository
            auto_mapping: false
            naming_strategy: doctrine.orm.naming_strategy.default
            quote_strategy: doctrine.orm.quote_strategy.default
            entity_listener_resolver: null
            repository_factory: doctrine.orm.container_repository_factory
            hydrators: {  }
            filters: {  }
        upv6:
            connection: upv6
            mappings:
                Upv6:
                    is_bundle: false
                    type: annotation
                    dir: /var/www/symfony/src/Entity/Upv6
                    prefix: App\Entity\Upv6
                    alias: Upv6
                    mapping: true
            query_cache_driver:
                type: array
                namespace: null
                cache_provider: null
            metadata_cache_driver:
                type: array
                namespace: null
                cache_provider: null
            result_cache_driver:
                type: array
                namespace: null
                cache_provider: null
            class_metadata_factory_name: Doctrine\ORM\Mapping\ClassMetadataFactory
            default_repository_class: Doctrine\ORM\EntityRepository
            auto_mapping: false
            naming_strategy: doctrine.orm.naming_strategy.default
            quote_strategy: doctrine.orm.quote_strategy.default
            entity_listener_resolver: null
            repository_factory: doctrine.orm.container_repository_factory
            hydrators: {  }
            filters: {  }
    resolve_target_entities: {  }


  • 답변 # 1

    이 문제는 Symfony가 구성 파일을 병합하는 방식으로 인해 발생합니다.

    /config/packages/*.yml  > /config/packages/<env>/*.yml

    이로 인해 packages/doctrine.yml 에 정의 된 모든 연결 및 엔티티 관리자가 발생합니다.  당신의 packages/test/doctrine.yml 에 추가되고

    Symfony가 사용할 병합 된 구성 설정을 보려면 다음을 실행하십시오 :

    php bin/console --env=test debug:config doctrine
    
    
    이 실행 bin/console --env=test doctrine:schema:create 때문에  결과 구성에있는엔티티 관리자모두에 대한 스키마를 생성하려고 시도합니다.

    이 문제를 해결하려면 환경 구성을 prod 로 분리해야합니다. testdev  또는 대안으로 .env.test 를 사용하십시오.  Syfmony에서 사용하는 URL을 변경합니다.

    엔티티 관리자에 어떻게 액세스해야하는지에 따라

    예 와이즈 비즈

    config/packages/doctrine.yml

    This is only anexampleof how to segregate the environment configurations, be sure to change any needed values for your application requirements

    config/packages/dev/doctrine.yml

    parameters:
        # Adds a fallback DATABASE_URL if the env var is not set.
        # This allows you to run cache:warmup even if your
        # environment variables are not available yet.
        # You should not need to change this value.
        env(DATABASE_URL): ''
    doctrine:
        orm:
            auto_generate_proxy_classes:  true # <- change to true
            proxy_dir:            '%kernel.cache_dir%/doctrine/orm/Proxies'
            proxy_namespace:      Proxies
    
    

    config/packages/test/doctrine.yml

    doctrine:
        dbal:
            # configure these for your database server
            connections:
                    gui:
                        driver: 'pdo_mysql'
                        server_version: '5.7'
                        charset: utf8mb4
                        default_table_options:
                            charset: utf8mb4
                            collate: utf8mb4_unicode_ci
                        url: '%env(resolve:DATABASE_GUI_URL)%'
                    upv6:
                        driver: 'pdo_mysql'
                        server_version: '5.7'
                        charset: utf8mb4
                        default_table_options:
                            charset: utf8mb4
                            collate: utf8mb4_unicode_ci
                        url: '%env(resolve:DATABASE_UPV6_URL)%'        
            entity_managers:
                gui:
                    connection: gui
                    mappings:
                        Gui:
                            is_bundle: false
                            type: annotation
                            dir: '%kernel.project_dir%/src/Entity/Gui'
                            prefix: 'App\Entity\Gui'
                            alias: Gui
                upv6:
                    connection: upv6
                    mappings:
                        Upv6:
                            is_bundle: false
                            type: annotation
                            dir: '%kernel.project_dir%/src/Entity/Upv6'
                            prefix: 'App\Entity\Upv6'
                            alias: Upv6
    
    

    config/packages/prod/doctrine.yml

    doctrine:
        dbal:
            driver: 'pdo_sqlite'
            url: 'sqlite:///%kernel.project_dir%/var/data/test.sqlite'
        #DEFINE THE ENTITY MANAGERS TO USE THE default CONNECTION
        #orm:
            #since the connections are not the same
            #you need to define your entity managers here...
            #entity_managers:
         #      gui:
         #          connection: default #<---- NOTICE DEFAULT and not gui 
         # (not sure what entity managers are needed for sqlite)
         #...
    
    
    imports: - { resource: '../dev/doctrine.yaml' } doctrine: orm: auto_generate_proxy_classes: false #<-- change to false metadata_cache_driver: type: service id: doctrine.system_cache_provider query_cache_driver: type: service id: doctrine.system_cache_provider result_cache_driver: type: service id: doctrine.result_cache_provider #...

관련 자료

  • 이전 c# - 반사 - 기본 방법을 얻는 방법 (원래 방법이 아님)?
  • 다음 AspNet MVC의 데이터 테이블에서 정렬을위한 여러 열의 이름 가져 오기