Edgewall Software

Ticket #2164 (closed defect: fixed)

Opened 3 years ago

Last modified 3 years ago

DB error when submitting Custom Query using custom fields against Postgres

Reported by: abudinsz@… Owned by: cmlenz
Priority: high Milestone: 0.9
Component: ticket system Version: 0.9b2
Severity: critical Keywords: postgresql custom query
Cc:

Description (last modified by cmlenz) (diff)

I added a single custom field in trac.ini called "customer". When attempting to submit a Custom Query where the "customer" field is used as a condition I get a DB error. This problem occured (with slightly different behavior) by using either of the two recommended postgresql python bindings.

Trac.ini (excerpt):

[ticket-custom]
customer = select
customer.label = Customer
customer.options = none|Company1|Company2|Company3|Company4
customer.value = 0

Environment:

  • PostgreSQL - 7.4.8
  • Python - 2.3.5
  • Apache - 2.0.46
  • Trac - 0.9b2 (also happened with 0.9b1)
Traceback:
Traceback (most recent call last):
  File "/opt/trac/lib/python2.3/site-packages/trac/web/modpython_frontend.py", line 206, in handler
    dispatch_request(mpr.path_info, mpr, env)
  File "/opt/trac/lib/python2.3/site-packages/trac/web/main.py", line 139, in dispatch_request
    dispatcher.dispatch(req)
  File "/opt/trac/lib/python2.3/site-packages/trac/web/main.py", line 107, in dispatch
    resp = chosen_handler.process_request(req)
  File "/opt/trac/lib/python2.3/site-packages/trac/ticket/query.py", line 393, in process_request
    self.display_html(req, query)
  File "/opt/trac/lib/python2.3/site-packages/trac/ticket/query.py", line 507, in display_html
    tickets = query.execute(db)
  File "/opt/trac/lib/python2.3/site-packages/trac/ticket/query.py", line 140, in execute
    cursor.execute(sql)
ProgrammingError: ERROR:  relation reference "customer" cannot be used in an expression

SELECT t.id AS id,t.summary AS summary,t.status AS status,t.owner AS owner,t.type AS type,t.priority AS priority,t.time AS time,t.changetime AS changetime,priority.value AS priority_value,customer.value AS customer
FROM ticket AS t
  LEFT OUTER JOIN ticket_custom AS customer ON (id=customer.ticket AND customer.name='customer')
  LEFT OUTER JOIN enum AS priority ON (priority.type='priority' AND priority.name=priority)
WHERE COALESCE(t.status,'') IN ('new','assigned','reopened') AND COALESCE(customer,'')='' AND COALESCE(t.owner,'')!='myuser'
ORDER BY COALESCE(t.priority,'')='',priority.value,t.id

Attachments

Change History

Changed 3 years ago by cmlenz

  • description modified (diff)

(reformatting of description)

Changed 3 years ago by anonymous

  • status changed from new to closed
  • resolution set to fixed

Index: /usr/src/trac-devel/trac/trac/ticket/query.py =================================================================== --- /usr/src/trac-devel/trac/trac/ticket/query.py (revision 2318) +++ /usr/src/trac-devel/trac/trac/ticket/query.py (working copy) @@ -211,6 +211,8 @@

value = sql_escape(value[len(mode and '!' or + mode):]) if name not in custom_fields:

name = 't.' + name

+ else: + name= name+'.value'

if mode == '~' and value:

return "COALESCE(%s,) %sLIKE '%%%s%%'" % (

name, neg and 'NOT ' or , value)

@@ -240,7 +242,7 @@

if k not in custom_fields:

col = 't.'+k

else:

- col = k + col = k+'.value'

clauses.append("COALESCE(%s,) %sIN (%s)"

% (col, neg and 'NOT ' or , inlist))

elif len(v) > 1:

@@ -269,7 +271,7 @@

if name not in custom_fields:

col = 't.'+name

else:

- col = name + col = name+'.value'

if name == 'id':

# FIXME: This is a somewhat ugly hack. Can we also have the # column type for this? If it's an integer, we do first

Changed 3 years ago by anonymous

  • usr/src/trac-devel/trac/trac/ticket/query.py

     
    211211            value = sql_escape(value[len(mode and '!' or '' + mode):]) 
    212212            if name not in custom_fields: 
    213213                name = 't.' + name 
     214           else: 
     215               name= name+'.value' 
    214216            if mode == '~' and value: 
    215217                return "COALESCE(%s,'') %sLIKE '%%%s%%'" % ( 
    216218                       name, neg and 'NOT ' or '', value) 
     
    240242                if k not in custom_fields: 
    241243                    col = 't.'+k 
    242244                else: 
    243                     col = k 
     245                    col = k+'.value' 
    244246                clauses.append("COALESCE(%s,'') %sIN (%s)" 
    245247                               % (col, neg and 'NOT ' or '', inlist)) 
    246248            elif len(v) > 1: 
     
    269271            if name not in custom_fields: 
    270272                col = 't.'+name 
    271273            else: 
    272                 col = name 
     274                col = name+'.value' 
    273275            if name == 'id': 
    274276                # FIXME: This is a somewhat ugly hack.  Can we also have the 
    275277                #        column type for this?  If it's an integer, we do first 

Changed 3 years ago by cmlenz

  • status changed from closed to reopened
  • resolution fixed deleted
  • milestone set to 0.9

This shouldn't be closed unless a fix is checked in.

Changed 3 years ago by cmlenz

  • component changed from report system to ticket system

Changed 3 years ago by cmlenz

  • owner changed from daniel to cmlenz
  • status changed from reopened to new

Changed 3 years ago by jnorthrup<AT>io-informatics.com

thanks for the quick notice. I have a rather aggressively patched postgres+timetracking v9 devel readonly tree scrutinized against pharma/FDA project requirements. Would gladly share my progress.

email me if this is interesting.

Changed 3 years ago by anonymous

  • milestone changed from 0.9 to 0.9.1

Changed 3 years ago by anonymous

  • milestone changed from 0.9.1 to 0.9

Changed 3 years ago by cmlenz

  • status changed from new to assigned

Changed 3 years ago by cmlenz

  • status changed from assigned to closed
  • resolution set to fixed

Patch applied in [2405]. Thanks!

Add/Change #2164 (DB error when submitting Custom Query using custom fields against Postgres)

Author



Change Properties
<Author field>
Action
as closed
Next status will be 'reopened'
 
Note: See TracTickets for help on using tickets.