| | 934 | Be careful when using the ``tables`` parameter if you're specifying |
|---|
| | 935 | tables that are already used in the query. When you add extra tables |
|---|
| | 936 | via the ``tables`` parameter, Django assumes you want that table included |
|---|
| | 937 | an extra time, if it is already included. That creates a problem, |
|---|
| | 938 | since the table name will then be given an alias. If a table appears |
|---|
| | 939 | multiple times in an SQL statement, the second and subsequent occurrences |
|---|
| | 940 | must use aliases so the database can tell them apart. If you're |
|---|
| | 941 | referring to the extra table you added in the extra ``where`` parameter |
|---|
| | 942 | this is going to cause errors. |
|---|
| | 943 | |
|---|
| | 944 | Normally you'll only be adding extra tables that don't already appear in |
|---|
| | 945 | the query. However, if the case outlined above does occur, there are a few |
|---|
| | 946 | solutions. First, see if you can get by without including the extra table |
|---|
| | 947 | and use the one already in the query. If that isn't possible, put your |
|---|
| | 948 | ``extra()`` call at the front of the queryset construction so that your |
|---|
| | 949 | table is the first use of that table. Finally, if all else fails, look at |
|---|
| | 950 | the query produced and rewrite your ``where`` addition to use the alias |
|---|
| | 951 | given to your extra table. The alias will be the same each time you |
|---|
| | 952 | construct the queryset in the same way, so you can rely upon the alias |
|---|
| | 953 | name to not change. |
|---|
| | 954 | |
|---|