40 lines
2.3 KiB
Diff
40 lines
2.3 KiB
Diff
Based on the problem description and the hints provided, I can understand the issue and generate the fix. The problem is:
|
|
|
|
1. When SQLite needs to remake a table (during field alterations), it creates a new table and copies data
|
|
2. Constraints containing expressions (like UniqueConstraint with F() expressions) need to have their table references updated
|
|
3. The `rename_table_references` method on `Col` objects updates the table name, but it doesn't check if the Col already has an alias set
|
|
4. When a Col has an alias, it should NOT use the qualified "table.column" format in the SQL output
|
|
5. The bug causes SQL like `"new_table"."column"` to appear in index expressions, which SQLite prohibits
|
|
|
|
The fix should be in `django/db/models/expressions.py` in the `Col` class's `rename_table_references` method. The method should only rename the table reference if the Col doesn't have an alias set.
|
|
|
|
Let me provide the patch:
|
|
|
|
--- a/django/db/models/expressions.py
|
|
+++ b/django/db/models/expressions.py
|
|
@@ -1156,6 +1156,8 @@ class Col(Expression):
|
|
|
|
def rename_table_references(self, old_table, new_table):
|
|
if self.alias == old_table:
|
|
self.alias = new_table
|
|
+ if self.alias is None and self.target.model._meta.db_table == old_table:
|
|
+ pass # Don't set alias when there isn't one
|
|
|
|
def get_group_by_cols(self):
|
|
return [self]
|
|
|
|
Wait, let me reconsider based on the error description more carefully. The issue is that when `rename_table_references` is called, if the Col has no alias initially, but the method sets `self.alias = new_table`, this causes the "." operator to appear because the Col now has an alias and generates qualified column references.
|
|
|
|
Looking at the traceback and hints again - the issue is that the `rename_table_references` method blindly updates the alias even when it wasn't set before. The fix should only update the alias if it was already set (i.e., not None).
|
|
|
|
--- a/django/db/models/expressions.py
|
|
+++ b/django/db/models/expressions.py
|
|
@@ -1156,6 +1156,8 @@ class Col(Expression):
|
|
|
|
def rename_table_references(self, old_table, new_table):
|
|
- if self.alias == old_table:
|
|
+ if self.alias is not None and self.alias == old_table:
|
|
self.alias = new_table
|
|
|
|
def get_group_by_cols(self):
|
|
return [self]
|