Version 7 (modified by trac, 7 years ago) (diff)

--

Trac Ticket Queries

In addition to reports, Trac provides support for custom ticket queries, used to display lists of tickets meeting a specified set of criteria.

To configure and execute a custom query, switch to the View Tickets module from the navigation bar, and select the Custom Query link.

Filters

When you first go to the query page the default filter will display tickets relevant to you:

  • If logged in then all open tickets it will display open tickets assigned to you.
  • If not logged in but you have specified a name or email address in the preferences then it will display all open tickets where your email (or name if email not defined) is in the CC list.
  • If not logged and no name/email defined in the preferences then all open issues are displayed.

Current filters can be removed by clicking the button to the left with the minus sign on the label. New filters are added from the pulldown lists at the bottom corners of the filters box ('And' conditions on the left, 'Or' conditions on the right). Filters with either a text box or a pulldown menu of options can be added multiple times to perform an or of the criteria.

You can use the fields just below the filters box to group the results based on a field, or display the full description for each ticket.

Once you've edited your filters click the Update button to refresh your results.

Clicking on one of the query results will take you to that ticket. You can navigate through the results by clicking the Next Ticket or Previous Ticket links just below the main menu bar, or click the Back to Query link to return to the query page.

You can safely edit any of the tickets and continue to navigate through the results using the Next/Previous/Back to Query links after saving your results. When you return to the query any tickets which were edited will be displayed with italicized text. If one of the tickets was edited such that it no longer matches the query criteria the text will also be greyed. Lastly, if a new ticket matching the query criteria has been created, it will be shown in bold.

The query results can be refreshed and cleared of these status indicators by clicking the Update button again.

Saving Queries

Trac allows you to save the query as a named query accessible from the reports module. To save a query ensure that you have Updated the view and then click the Save query button displayed beneath the results. You can also save references to queries in Wiki content, as described below.

Note: one way to easily build queries like the ones below, you can build and test the queries in the Custom report module and when ready - click Save query. This will build the query string for you. All you need to do is remove the extra line breaks.

Note: you must have the REPORT_CREATE permission in order to save queries to the list of default reports. The Save query button will only appear if you are logged in as a user that has been granted this permission. If your account does not have permission to create reports, you can still use the methods below to save a query.

You may want to save some queries so that you can come back to them later. You can do this by making a link to the query from any Wiki page.

[query:status=new|assigned|reopened&version=1.0 Active tickets against 1.0]

Which is displayed as:

Active tickets against 1.0

This uses a very simple query language to specify the criteria (see Query Language).

Alternatively, you can copy the query string of a query and paste that into the Wiki link, including the leading ? character:

[query:?status=new&status=assigned&status=reopened&group=owner Assigned tickets by owner]

Which is displayed as:

Assigned tickets by owner

Using the [[TicketQuery]] Macro

The TicketQuery macro lets you display lists of tickets matching certain criteria anywhere you can use WikiFormatting.

Example:

[[TicketQuery(version=0.6|0.7&resolution=duplicate)]]

This is displayed as:

No results

Just like the query: wiki links, the parameter of this macro expects a query string formatted according to the rules of the simple ticket query language. This also allows displaying the link and description of a single ticket:

[[TicketQuery(id=123)]]

This is displayed as:

#123
Typo in the model_api/#field-types

A more compact representation without the ticket summaries is also available:

[[TicketQuery(version=0.6|0.7&resolution=duplicate, compact)]]

This is displayed as:

No results

Finally, if you wish to receive only the number of defects that match the query, use the count parameter.

[[TicketQuery(version=0.6|0.7&resolution=duplicate, count)]]

This is displayed as:

0

Customizing the table format

You can also customize the columns displayed in the table format (format=table) by using col=<field> - you can specify multiple fields and what order they are displayed by placing pipes (|) between the columns like below:

[[TicketQuery(max=3,status=closed,order=id,desc=1,format=table,col=resolution|summary|owner|reporter)]]

This is displayed as:

Full rows

In table format you can also have full rows by using rows=<field> like below:

[[TicketQuery(max=3,status=closed,order=id,desc=1,format=table,col=resolution|summary|owner|reporter,rows=description)]]

This is displayed as:

Results (1 - 3 of 28229)

1 2 3 4 5 6 7 8 9 10 11
Ticket Resolution Summary Owner Reporter
#30384 invalid Django 1.11 Upgrade -> runserver Error in db/models/fields/related.py _check_table_uniqueness nobody Eric Neumann
Description

Hello,

I'm currently upgrading from Django 1.10 to 1.11. I've addressed all deprecation issues and everything is working fine, our app is running on gunicorn and django shell works. However, runserver is throwing an error that I can't seem to sort out. Looks like it's happening on the related groups field to the user model. We are using a custom user model which was working fine in 1.10. Below is the stack trace. Appreciate any help in getting this resolved. Cheers!

Unhandled exception in thread started by <function wrapper at 0x7f6d8b8358c0> Traceback (most recent call last):

File "/home/manzama/.virtualenvs/manzama/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 227, in wrapper

fn(*args, kwargs)

File "/home/manzama/.virtualenvs/manzama/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run

self.check(display_num_errors=True)

File "/home/manzama/.virtualenvs/manzama/local/lib/python2.7/site-packages/django/core/management/base.py", line 359, in check

include_deployment_checks=include_deployment_checks,

File "/home/manzama/.virtualenvs/manzama/local/lib/python2.7/site-packages/django/core/management/base.py", line 346, in _run_checks

return checks.run_checks(kwargs)

File "/home/manzama/.virtualenvs/manzama/local/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks

new_errors = check(app_configs=app_configs)

File "/home/manzama/.virtualenvs/manzama/local/lib/python2.7/site-packages/django/core/checks/model_checks.py", line 30, in check_all_models

errors.extend(model.check(kwargs))

File "/home/manzama/.virtualenvs/manzama/local/lib/python2.7/site-packages/django/db/models/base.py", line 1282, in check

errors.extend(cls._check_fields(kwargs))

File "/home/manzama/.virtualenvs/manzama/local/lib/python2.7/site-packages/django/db/models/base.py", line 1359, in _check_fields

errors.extend(field.check(from_model=cls, kwargs))

File "/home/manzama/.virtualenvs/manzama/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1195, in check

errors.extend(self._check_table_uniqueness(kwargs))

File "/home/manzama/.virtualenvs/manzama/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1446, in _check_table_uniqueness

for model in self.opts.apps.get_models(include_auto_created=True)

File "/home/manzama/.virtualenvs/manzama/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1447, in <dictcomp>

if model != self.remote_field.through and model._meta.managed

AttributeError: 'ModelOptions' object has no attribute 'managed'

#30381 duplicate django/contrib/auth/migrations/0011_update_proxy_permissions.py can cause a unique constraint violation nobody Jon Dufresne
Description

Use an existing database that was migrated using Django 2.1. This must contain auth permissions for proxy models. Per Django 2.1, Permission.content_type points to the concrete model's content type, not the proxy's.

Once this database exists, steps to reproduce:

  1. Run a subset of migrations that includes the proxy model's app but does not include 0011_update_proxy_permissions.
  2. This triggers the auth create_permissions() post migrate handler.
  3. create_permissions() generates new permissions for the proxy models that point to the proxy content type. The old ones that point to the concrete model still exist.
  4. Now run all migrations.
  5. auth.0011_update_proxy_permissions runs and tries to update the pre-existing proxy models. But as the auth permissions were already created during the previous migration run, this crashes with a unique constraint violation.

Traceback (PostgreSQL backend):

Traceback (most recent call last):
  ...
  File ".../venv/lib64/python3.7/site-packages/django/core/management/commands/migrate.py", line 234, in handle
    fake_initial=fake_initial,
  File ".../venv/lib64/python3.7/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File ".../venv/lib64/python3.7/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File ".../venv/lib64/python3.7/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File ".../venv/lib64/python3.7/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File ".../venv/lib64/python3.7/site-packages/django/db/migrations/operations/special.py", line 190, in database_forwards
    self.code(from_state.apps, schema_editor)
  File ".../venv/lib64/python3.7/site-packages/django/contrib/auth/migrations/0011_update_proxy_permissions.py", line 30, in update_proxy_model_permissions
    ).update(content_type=new_content_type)
  File ".../venv/lib64/python3.7/site-packages/django/db/models/query.py", line 741, in update
    rows = query.get_compiler(self.db).execute_sql(CURSOR)
  File ".../venv/lib64/python3.7/site-packages/django/db/models/sql/compiler.py", line 1426, in execute_sql
    cursor = super().execute_sql(result_type)
  File ".../venv/lib64/python3.7/site-packages/django/db/models/sql/compiler.py", line 1097, in execute_sql
    cursor.execute(sql, params)
  File ".../venv/lib64/python3.7/site-packages/django/db/backends/utils.py", line 99, in execute
    return super().execute(sql, params)
  File ".../venv/lib64/python3.7/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File ".../venv/lib64/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File ".../venv/lib64/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File ".../venv/lib64/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File ".../venv/lib64/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "auth_permission_content_type_id_01ab375a_uniq"
DETAIL:  Key (content_type_id, codename)=(168, add_myproxymodel) already exists.
#30379 duplicate ProgrammingError from exclude using models.F with long relation nobody Peter Law
Description

The following query emits a ProgrammingError due to the query omitting the definitions of a number of references it uses:

Alert.objects.exclude(
    item__order_items__order__user=models.F('user'),
)
django.db.utils.ProgrammingError: missing FROM-clause entry for table "u2"

The spelling using a specific user does seem to work correctly, hinting that the issue is somehow related to the handling of the models.F component.

Alert.objects.exclude(
    item__order_items__order__user=the_user,
)

Models:

from django.db import models

class Item(models.Model):
    name = models.TextField()

class Order(models.Model):
    user = models.ForeignKey('auth.User', on_delete=models.CASCADE)

class UnsizedOrderItem(models.Model):
    order = models.ForeignKey(Order, on_delete=models.CASCADE, related_name='items')
    item = models.ForeignKey(Item, on_delete=models.CASCADE, related_name='order_items')

class Alert(models.Model):
    user = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    item = models.ForeignKey(Item, on_delete=models.CASCADE, related_name='alerts')

I originally saw this with another layer of many-to-many between the item and the order, but that turned out not to be necessary to reproduce the issue:

class SizedItem(models.Model):
    item = models.ForeignKey(Item, on_delete=models.CASCADE, related_name='sizes')
    size = models.TextField()

class SizedOrderItem(models.Model):
    order = models.ForeignKey(Order, on_delete=models.CASCADE, related_name='sized_items')
    sized_item = models.ForeignKey(SizedItem, on_delete=models.CASCADE, related_name='sized_order_items')

Alert.objects.exclude(
    item__sizes__sized_order_items__order__user=models.F('user'),
)

Traceback:

  File ".../lib/python3.5/site-packages/django/db/models/query.py", line 55, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File ".../lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1097, in execute_sql
    cursor.execute(sql, params)
  File ".../lib/python3.5/site-packages/sentry_sdk/integrations/django/__init__.py", line 298, in execute
    return real_execute(self, sql, params)
  File ".../lib/python3.5/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File ".../lib/python3.5/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File ".../lib/python3.5/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File ".../lib/python3.5/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File ".../lib/python3.5/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: missing FROM-clause entry for table "u2"
LINE 1: ...JOIN "liked_email_alerts_unsizedorderitem" U3 ON (U2."id" = ...
                                                             ^

Versions:

  • I've seen this under both 2.1.7 and 2.2
  • I'm using Python 3.5.2
  • I'm using Postgres 9.6 (psycopg2-binary==2.7.7)

For reference here's the SQL generated by printing the .query on the querysets:

-- relative

SELECT
    "liked_email_alerts_alert"."id",
    "liked_email_alerts_alert"."user_id",
    "liked_email_alerts_alert"."item_id"
FROM
    "liked_email_alerts_alert"
WHERE
    NOT ("liked_email_alerts_alert"."item_id" IN (
            SELECT
                U3. "item_id"
            FROM
                "liked_email_alerts_alert" U0
                INNER JOIN "liked_email_alerts_unsizedorderitem" U3 ON (U2. "id" = U3. "item_id")
                INNER JOIN "liked_email_alerts_order" U4 ON (U3. "order_id" = U4. "id")
            WHERE
                U4. "user_id" = (U0. "user_id")));

-- specific

SELECT
    "liked_email_alerts_alert"."id",
    "liked_email_alerts_alert"."user_id",
    "liked_email_alerts_alert"."item_id"
FROM
    "liked_email_alerts_alert"
WHERE
    NOT ("liked_email_alerts_alert"."item_id" IN (
            SELECT
                U2. "item_id"
            FROM
                "liked_email_alerts_unsizedorderitem" U2
                INNER JOIN "liked_email_alerts_order" U3 ON (U2. "order_id" = U3. "id")
            WHERE
                U3. "user_id" = 13))
1 2 3 4 5 6 7 8 9 10 11

Query Language

query: TracLinks and the [[TicketQuery]] macro both use a mini “query language” for specifying query filters. Basically, the filters are separated by ampersands (&). Each filter then consists of the ticket field name, an operator, and one or more values. More than one value are separated by a pipe (|), meaning that the filter matches any of the values. To include a literal & or | in a value, escape the character with a backslash (\).

The available operators are:

= the field content exactly matches one of the values
~= the field content contains one or more of the values
^= the field content starts with one of the values
$= the field content ends with one of the values

All of these operators can also be negated:

!= the field content matches none of the values
!~= the field content does not contain any of the values
!^= the field content does not start with any of the values
!$= the field content does not end with any of the values

The date fields created and modified can be constrained by using the = operator and specifying a value containing two dates separated by two dots (..). Either end of the date range can be left empty, meaning that the corresponding end of the range is open. The date parser understands a few natural date specifications like "3 weeks ago", "last month" and "now", as well as Bugzilla-style date specifications like "1d", "2w", "3m" or "4y" for 1 day, 2 weeks, 3 months and 4 years, respectively. Spaces in date specifications can be left out to avoid having to quote the query string.

created=2007-01-01..2008-01-01 query tickets created in 2007
created=lastmonth..thismonth query tickets created during the previous month
modified=1weekago.. query tickets that have been modified in the last week
modified=..30daysago query tickets that have been inactive for the last 30 days

See also: TracTickets, TracReports, TracGuide

Back to Top