【问题标题】:Doctrine\DBAL\Schema\SchemaException: How to include one or more FKs in a declared table indexDoctrine\DBAL\Schema\SchemaException:如何在声明的表索引中包含一个或多个 FK
【发布时间】:2016-05-14 12:39:54
【问题描述】:

我正在使用 Symfony/YaML/Doctrine 将数据存储在 MySQL 后端。我想使用我声明的一些 FK 创建一个索引(以加快查询速度)。但是,当我尝试生成架构时,出现以下错误:

[Doctrine\DBAL\Schema\SchemaException] 没有具有名称的列 表“pms”上的“to_user_id”

当我注释掉索引声明时,我可以毫无问题地生成表。这是我的 YaML 文件的样子:

AppBundle\Entity\PrivateMessage:
    type: entity
    table: pms
    repositoryClass: PMSRepository
    id:
        id:
            type: integer
            generator: { strategy: AUTO }

    manyToOne:
        from_user:
            targetEntity: User
            joinColumn:
                name: from_user_id
                referencedColumnName: id
                nullable: false
                onDelete: RESTRICT
                onUpdate: CASCADE

    manyToOne:
        to_user:
            targetEntity: User
            joinColumn:
                name: to_user_id
                referencedColumnName: id
                nullable: false
                onDelete: RESTRICT
                onUpdate: CASCADE                

    manyToOne:
        message_type:
            targetEntity: PrivateMessageType
            joinColumn:
                name: pms_type_id
                referencedColumnName: id
                nullable: false
                onDelete: RESTRICT
                onUpdate: CASCADE  


    manyToOne:
        message_status:
            targetEntity: PrivateMessageStatus
            joinColumn:
                name: pms_status_id
                referencedColumnName: id
                nullable: false
                onDelete: RESTRICT
                onUpdate: CASCADE  

    indexes:
        idx_pms:
            columns: [from_user_id, to_user_id, created_at]          


    fields:
        subject:
            type: text
            nullable: false

        body:
            type: text
            nullable: false

        attach_1:
            type: string
            length: 128
            nullable: true

        attach_2:
            type: string
            length: 128
            nullable: true

        attach_3:
            type: string
            length: 128
            nullable: true

        created_at:
            type: datetime           

【问题讨论】:

    标签: symfony doctrine-orm yaml


    【解决方案1】:

    问题很简单:不要将所有关联拆分为几个manyToOne。正确的变体应该是这样的

    AppBundle\Entity\PrivateMessage:
        type: entity
        table: pms
        repositoryClass: PMSRepository
        id:
            id:
                type: integer
                generator: { strategy: AUTO }
    
        manyToOne:
            from_user:
                targetEntity: User
                joinColumn:
                    name: from_user_id
                    referencedColumnName: id
                    nullable: false
                    onDelete: RESTRICT
                    onUpdate: CASCADE
            to_user:
                targetEntity: User
                joinColumn:
                    name: to_user_id
                    referencedColumnName: id
                    nullable: false
                    onDelete: RESTRICT
                    onUpdate: CASCADE                
            message_type:
                targetEntity: PrivateMessageType
                joinColumn:
                    name: pms_type_id
                    referencedColumnName: id
                    nullable: false
                    onDelete: RESTRICT
                    onUpdate: CASCADE  
            message_status:
                targetEntity: PrivateMessageStatus
                joinColumn:
                    name: pms_status_id
                    referencedColumnName: id
                    nullable: false
                    onDelete: RESTRICT
                    onUpdate: CASCADE  
    
        indexes:
            idx_pms:
                columns: [from_user_id, to_user_id, created_at]          
    
    
        fields:
            subject:
                type: text
                nullable: false
    
            body:
                type: text
                nullable: false
    
            attach_1:
                type: string
                length: 128
                nullable: true
    
            attach_2:
                type: string
                length: 128
                nullable: true
    
            attach_3:
                type: string
                length: 128
                nullable: true
    
            created_at:
                type: datetime           
    

    【讨论】:

    • 嘿嘿嘿,今天早上睡了一晚,我自己想通了!
    猜你喜欢
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多