Search code examples
sqlconstraintsfieldlist

SQLSTATE[23000]: Integrity constraint violation: 1052 Column : 'quantite' in field list is ambiguous


I have referenced other questions like mine, but I cannot figure out why I am getting the error in my example.

I have a method that looks like this :

$select = $this->select()->setIntegrityCheck(false)

                            ->from(array('dmas' => $this->_name), array(
                                'id',
                                'is_new',
                            'id_demande',
                            'id_ds',
                            'id_station',
                            'id AS DT_RowId',
                            'quantite',
                            'quantite as quantite_initiale',
                            'quantite_voulue' => new Zend_Db_Expr("quantite - 
                            coalesce(quantite_attribuee, 0)"),
                            'quantite_attribuee',
                            'is_attribue',
                            'num_station',
                            'id_catalog'
                            ))

                        ->joinLeft(array('cal' => 'catalog_accessory_list'), 
                        'cal.id = dmas.id_catalog', array('id_function1 as id_function_1', 
                        'id_function2 as id_function_2', 'id_function3 as id_function_3'))
                        ->joinLeft(array('cam' => 'catalog_accessory_model'), 'cam.id = cal.id_model', array('name as nom_model', 'id as id_model'))
                        ->joinLeft(array('caf1' => 'catalog_accessory_function1'), 'caf1.id = cal.id_function1', array('name as name_function_1'))
                        ->joinLeft(array('caf2' => 'catalog_accessory_function2'), 'caf2.id = cal.id_function2', array('name as name_function_2'))
                        ->joinLeft(array('caf3' => 'catalog_accessory_function3'), 'caf3.id = cal.id_function3', array('name as name_function_3',
                                'full_designation' => new Zend_Db_Expr("CONCAT_WS(' | ', cam.name, caf1.name, caf2.name, caf3.name)")))

                        ->joinLeft(array('ds' => 'demande_station'), 'ds.id = dmas.id_ds', array('date_disponibilite' => new Zend_Db_Expr("DATE_FORMAT(ds.disponibilite,'%d/%m/%Y')"),'disponibilite' => new Zend_Db_Expr("DATE_FORMAT(ds.disponibilite,'%d/%m/%Y')"),'date_debut_chantier' => new Zend_Db_Expr("DATE_FORMAT(ds.debut_chantier,'%d/%m/%Y')"),'date_fin_chantier' => new Zend_Db_Expr("DATE_FORMAT(ds.fin_chantier,'%d/%m/%Y')")));

The above query works perfectly. However, when I try to add this joinLeft I get the error:

->joinLeft(array('a' => 'accessoire'), 'a.id_catalog = dmas.id_catalog', array('id as DT_RowId','nom as code', 'a.quantite as quantite_stock'))
        ->where('(CAST(dmas.quantite AS SIGNED) - CAST(ifnull(dmas.quantite_attribuee, 0) AS SIGNED)) > 0 '
                . 'AND dmas.quantite > 0 AND (dmas.is_attribue <> 1 OR dmas.is_attribue is NULL) '
                . 'AND ds.accessoire_valid = 0'
                );

The error :

SQLSTATE[23000]: Integrity constraint violation: 1052 Column : 'quantite' in field list is ambiguous


Solution

  • Seems to me you need to explicitly qualify this: a.quantite or dmas.quantite. Your introduction of a table with the same column name has now broken the ability for implicit qualification from the dmas alias.

    enter image description here