Doctrine \ DBAL \ Schema \ SchemaException:如何在声明的表索引中包含一个或多个FK

时间:2016-05-14 12:39:54

标签: symfony doctrine-orm yaml

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

  

[Doctrine \ DBAL \ Schema \ SchemaException]没有名称的列   ' to_user_id'在桌子上' pms'

当我注释掉索引声明时,我能够毫无问题地生成表格。这就是我的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           

1 个答案:

答案 0 :(得分:0)

问题很简单:不要拆分多个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