使用PostgreSQL的Doctrine2 ManyToMany关系:" SQLSTATE [42P01]:未定义的表:7错误:关系不存在"

时间:2016-09-06 10:46:44

标签: postgresql symfony doctrine-orm

当我在两个实体之间进行INNER JOIN(由 ManyToMany 关系连接)时,说明无法找到关系 machine_table_for_base_model SQLSTATE[42P01]: Undefined table: 7 ERROR: the relation "machine_table_for_base_model" does not exist。为什么会这样?

我的associative table定义如下:

 <!-- language: lang-sql -->

CREATE TABLE machine_table_for_base_model ( 
    id_machine_table integer NOT NULL, 
    id_base_model integer NOT NULL, 
); 

 ALTER TABLE ONLY machine_table_for_base_model 
     ADD CONSTRAINT machine_table_for_base_model_pkey PRIMARY KEY (id_machine_table, id_base_model); 

 ALTER TABLE ONLY machine_table_for_base_model 
     ADD CONSTRAINT machine_table_for_base_model_ibfk_1 FOREIGN KEY (id_machine_table) REFERENCES machine_table(id) ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED; 

 ALTER TABLE ONLY machine_table_for_base_model 
     ADD CONSTRAINT machine_table_for_base_model_ibfk_2 FOREIGN KEY (id_base_model) REFERENCES base_model(id) ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED;

当我执行关联表的学说映射时,我有类似的东西:

Package\MyBundle\Entity\MachineTable:
    type: entity
    schema: techguide
    table: machine_table
    indexes:
        IDX_8386B61E3C5569AA:
            columns:
                - edit_user
    id:
        id:
            type: integer
            nullable: false
            options:
                unsigned: false
            id: true
            generator:
                strategy: SEQUENCE
    fields:
        description:
            type: string
            nullable: false
            length: 200
            options:
                fixed: false
        labelsDimensions:
            type: text
            nullable: false
            length: null
            options:
                fixed: false
            column: labels_dimensions
        editDate:
            type: datetime
            nullable: false
            options:
                default: now
            column: edit_date
    manyToOne:
        editUser:
            targetEntity: Package\SupportBundle\Entity\Account
            cascade: {  }
            fetch: LAZY
            mappedBy: null
            inversedBy: null
            joinColumns:
                edit_user:
                    referencedColumnName: id
            orphanRemoval: false
    manyToMany:
        idBaseModel:
            targetEntity: BaseModel
            cascade: {  }
            fetch: LAZY
            mappedBy: null
            inversedBy: idMachineTable
            joinTable:
                name: machine_table_for_base_model
                joinColumns:
                    -
                        name: id_machine_table
                        referencedColumnName: id
                inverseJoinColumns:
                    -
                        name: id_base_model
                        referencedColumnName: id
            orderBy: null
    lifecycleCallbacks: {  }

1 个答案:

答案 0 :(得分:0)

最后我找到了解决这个问题的方法。您需要在ORM配置上编辑(手动joinTable名称,在我的情况下是Yaml文件,添加关联表所属的Postgres模式:

<强> Entity.orm.yml

manyToMany:
    idBaseModel:
        targetEntity: BaseModel
        cascade: {  }
        fetch: LAZY
        mappedBy: null
        inversedBy: idMachineTable
        joinTable:
            name: <NAME_OF_YOUR_SCHEMA>.<NAME_OF_THE_TABLE>
            joinColumns:
                -
                    name: id_machine_table
                    referencedColumnName: id
            inverseJoinColumns:
                -
                    name: id_base_model
                    referencedColumnName: id
        orderBy: null
lifecycleCallbacks: {  }