django-pyodbc-azure
django-pyodbc-azure copied to clipboard
Generated query got table prefix in front of field names
from django.db import connection print(Asset.objects.all().query) SELECT "Asset"."AssetId", "Asset"."PlantNo", "Asset"."Description", "Asset"."SerialNumber", "Asset"."EngineNumber", "Asset"."PlateNumber", "Asset"."StartDate", "Asset"."Disabled", "Asset"."Responsibility" FROM "Asset"
You say that like it's a bad thing.
Well, yes this is a bad thing for me. At least when i get such error because of this.
a = Asset.objects.get(plantno='8001') Traceback (most recent call last): File "C:\Venvs\DjangoDevelopment\lib\site-packages\django\db\backends\utils.py", line 65, in execute return self.cursor.execute(sql, params) File "C:\Venvs\DjangoDevelopment\lib\site-packages\sql_server\pyodbc\base.py", line 545, in execute return self.cursor.execute(sql, params) pyodbc.ProgrammingError: ('42S02', "[42S02] [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'Asset'. (208) (SQLExecDirectW)")
Do you know how to add schema name in front of the table name? I think my problem is because i've schema in my database. If I could make my query like "Geo"."Asset"."AssetId"
Yes, a schema would probably explain the error.
I'd try one of two things:
-
If possible, make 'Geo' the default schema for the relevant user. I haven't actually played with SQL Server in a long time, and if this suggestion makes no sense, please ignore it.
-
add to your model a Meta class setting the table name:
class Asset(models.Model):
class Meta:
db_table = '"Geo"."Asset"'
or
db_table = 'Geo"."Asset'
The first is supposed to work because the backend is not supposed to mess with
a name if it starts and ends with double quotes. The second might also work.
plain 'Geo.Asset'
will not work, because the backend adds quotes when they
are not present.
HTH, Shai.
PS adding Schema support to django has been an open feature request for years: https://code.djangoproject.com/ticket/6148