Relationship Graph

Relationship Graph
related to related to child of child of duplicate of duplicate of

View Issue Details

IDProjectCategoryView StatusLast Update
0011139mantisbtcustom fieldspublic2011-08-05 02:36
Reporterleowmjw Assigned Toatrol  
PrioritynormalSeveritymajorReproducibilityalways
Status closedResolutionduplicate 
PlatformApache 2, PHP 5.2.4-2ubuntu5.7OSUbuntu Linux OS Version8.04
Product Version1.2.0a3 
Summary0011139: APPLICATION ERROR 0000401 - DB -> #1066: Not unique table/alias for custom_field
Description

Tested on v1.2.0a3 but reviewed code in v1.2.0rc2 but don't see any fix yet.

Description;

When trying to sort and then filter a custom field of type DATE; encounter APPLICATION ERROR 0000401 which is caused by DB Error #1066: Not unique table/alias.

Exception:

APPLICATION ERROR 0000401

Database query failed. Error received from database was #1066: Not unique table/alias: 'mantis_custom_field_string_table_9' for the query: SELECT Count( DISTINCT mantis_bug_table.id ) as idcnt FROM mantis_project_table, mantis_bug_table LEFT JOIN mantis_custom_field_string_table mantis_custom_field_string_table_9 ON mantis_custom_field_string_table_9.bug_id = mantis_bug_table.id AND mantis_custom_field_string_table_9.field_id = 9 LEFT JOIN mantis_custom_field_string_table mantis_custom_field_string_table_6 ON mantis_custom_field_string_table_6.bug_id = mantis_bug_table.id AND mantis_custom_field_string_table_6.field_id = 6 LEFT JOIN mantis_custom_field_string_table mantis_custom_field_string_table_9 ON mantis_bug_table.id = mantis_custom_field_string_table_9.bug_id AND mantis_custom_field_string_table_9.field_id = 9 WHERE mantis_project_table.enabled = 1 AND mantis_project_table.id = mantis
_bug_table.project_id AND ( mantis_bug_table.project_id = 5 ) AND (mantis_custom_field_string_table_9.value = '9 am') AND (mantis_custom_field_string_table_6.value = 'Jalan Ampang').

Steps:

Assumption: Have Custom Field of type DATE and Custom Field can be filtered and is show in the "View Issue" page.
1) Click on the custom field (for example: Appointment Date) in so that it becomes sorted (ASC/DESC).
2) Select filter on that same custom field (for example: Appointment Date On or Before Nov 10 2009. Click "Apply Filter". Note exception as above.

Root Cause:

Table mantis_custom_field_string_table aliased more than once in the LEFT JOIN portion.

Solution:

Patch for v1.2.0a3 (Ugly hack! Please suggest better alternative)

diff /home/leow/SANDBOX/MANTIS/support/current/core_v1.2.0a3/filter_api.php /home/leow/SANDBOX/MANTIS/mantis12/core/filter_api.php
1730a1731

  $uniq_hash = 0;

1744c1745,1748
< $t_table_name = $t_custom_field_stringtable . '' . $t_cfid;

          $t_table_name = $t_custom_field_string_table . '_' . $t_cfid; 
          $uniq_hash++;
          $t_table_name .= '_' . $uniq_hash;

1847a1852

=========================================================================

Steps To Reproduce

Steps:

Assumption: Have Custom Field of type DATE and Custom Field can be filtered and is show in the "View Issue" page.
1) Click on the custom field (for example: Appointment Date) in so that it becomes sorted (ASC/DESC).
2) Select filter on that same custom field (for example: Appointment Date On or Before Nov 10 2009. Click "Apply Filter". Note exception as above.

Additional Information

Root Cause:

Table mantis_custom_field_string_table aliased more than once in the LEFT JOIN portion.

Solution:

Patch for v1.2.0a3 (Ugly hack! Please suggest better alternative)

diff /home/leow/SANDBOX/MANTIS/support/current/core_v1.2.0a3/filter_api.php /home/leow/SANDBOX/MANTIS/mantis12/core/filter_api.php
1730a1731

  $uniq_hash = 0;

1744c1745,1748
< $t_table_name = $t_custom_field_stringtable . '' . $t_cfid;

          $t_table_name = $t_custom_field_string_table . '_' . $t_cfid; 
          $uniq_hash++;
          $t_table_name .= '_' . $uniq_hash;

1847a1852

=========================================================================

TagsNo tags attached.

Relationships

duplicate of 0005859 closeddaryn "Sort by" can't handle more than one custom field in combination 

Activities

leowmjw

leowmjw

2010-02-17 00:19

reporter   ~0024400

Updated patch for mantisbt-1.2.0rc2-2010-01-05-master-1.2.x-67762d7

Solution:

Patch for v1.2.0rc2 (Ugly hack! Please suggest better alternative)

leow@leow-laptop:~/SANDBOX/MANTIS$ diff mantis12build/core/filter_api.php pokokhydro/custom/core/filter_api.php
908a909

$uniq_hash = 0;
921a923,925
$uniq_hash++;
$t_cf_tablealias .= '' . $uniq_hash;

=========================================================================

Wepl

Wepl

2010-02-23 11:45

reporter   ~0024476

I have the same problem, thanks for the workaround which helps.

jsoula

jsoula

2010-06-03 10:03

reporter   ~0025681

I have the same problem with the version 1.2.0 and a mysql database.
I confirm all steps to reproduce with a custom field of type TEXT.

Babbage

Babbage

2010-07-30 17:10

reporter   ~0026185

Last edited: 2010-07-30 17:10

I noticed that the two LEFT JOINs that are causing the error, were actually the same JOIN, but were structured in a different way. I managed to track down where the second JOIN was being produced and came up with the following patch:

===================================================================
Index: filter_api.php

--- filter_api.php (revision 6)
+++ filter_api.php (working copy)
@@ -924,7 +924,7 @@

check to be sure this field wasn't already added to the query.

            if( !in_array( $t_cf_select, $p_query_clauses['select'] ) ) {
                $p_query_clauses['select'][] = $t_cf_select;
  • $p_query_clauses['join'][] = "LEFT JOIN $t_custom_field_string_table $t_cf_table_alias ON $t_bug_table.id = $t_cf_table_alias.bug_id AND $t_cf_table_alias.field_id = $t_custom_field_id";
  • $p_query_clauses['join'][] = "LEFT JOIN $t_custom_field_string_table $t_cf_table_alias ON $t_cf_table_alias.bug_id = $t_bug_table.id AND $t_cf_table_alias.field_id = $t_custom_field_id ";
    }

            $p_query_clauses['order'][] = $c_cf_alias . ' ' . $c_dir;

    ===================================================================

On my system (Mantis 1.2.1), this appears to have fixed the problem.

nimmich

nimmich

2010-10-05 04:58

reporter   ~0026952

This is a duplicate of 0005859 and not fixed yet.

My patch for this problem (1.2.0 - 1.2.2) is to prevent multiple left joins:

--- mantis-1.2.0/core/filter_api.php 2010-02-24 10:34:19.642476000 +0100
+++ mantis/core/filter_api.php 2010-10-05 09:55:54.372631700 +0200
@@ -924,7 +924,13 @@

check to be sure this field wasn't already added to the query.

            if( !in_array( $t_cf_select, $p_query_clauses['select'] ) ) {
                $p_query_clauses['select'][] = $t_cf_select;
  • $p_query_clauses['join'][] = "LEFT JOIN $t_custom_field_string_table $t_cf_table_alias ON $t_bug_table.id = $t_cf_table_alias.bug_id AND $t_cf_table_alias.field_id = $t_custom_field_id";
  • be sure to add join clause only if it isn't already there

  • WARNING: MUST be the same as in filter_get_bug_rows(), section custom field filters.

  • if you change here, also change there

  • $t_cf_join_clause = "LEFT JOIN $t_custom_field_string_table $t_cf_table_alias ON $t_cf_table_alias.bug_id = $t_bug_table.id AND $t_cf_table_alias.field_id = $t_custom_field_id ";
  • if( !in_array( $t_cf_join_clause, $p_query_clauses['join'] ) ) {
  • $p_query_clauses['join'][] = $t_cf_join_clause;
  • }
    }

            $p_query_clauses['order'][] = $c_cf_alias . ' ' . $c_dir;

    @@ -1857,6 +1863,9 @@

    Each custom field will result in a exponential growth like Number_of_Issues^Number_of_Custom_Fields

            # and only after this process ends (if it is able to) the result query will be filtered
            # by the WHERE clause and by the DISTINCT clause
  • WARNING: join clause MUST be the same as in filter_get_query_sort_data()

  • if you change here, also change there

            $t_cf_join_clause = "LEFT JOIN $t_custom_field_string_table $t_table_name ON $t_table_name.bug_id = $t_bug_table.id AND $t_table_name.field_id = $t_cfid ";
    
            if( $t_def['type'] == CUSTOM_FIELD_TYPE_DATE ) {

It may be better to use the table name of the joined table as a key (i. e. $p_query_clauses['join'][$t_cf_table_alias] instead of just $p_query_clauses['join'][]), but this would mean to rewrite some code in filter_get_bug_rows() (don't use array_push()).

atrol

atrol

2010-12-22 05:08

developer   ~0027659

thanks nimmich for the hints and the patch