2016-07-04 89 views
0

我試圖從我的django在mac osx上連接到azure sql服務。 當我做以下Django增加了方括號查詢SQL服務器

from photo_mapper.models import Restaurant 
rest_query = Restaurant.objects.all().query 
print rest_query 
SELECT [ate_it.restaurant].[id], [ate_it.restaurant].[name], [ate_it.restaurant].[area], [ate_it.restaurant].[city], [ate_it.restaurant].[submitter], [ate_it.restaurant].[pending], [ate_it.restaurant].[zid] FROM [ate_it.restaurant] 

由於它當我做Restaurant.objects.all()我得到

Traceback (most recent call last): 
    File "<console>", line 1, in <module> 
    File "/Users/anubhav/workspace/ate-it/venv/lib/python2.7/site-packages/django/db/models/query.py", line 234, in __repr__ 
    data = list(self[:REPR_OUTPUT_SIZE + 1]) 
    File "/Users/anubhav/workspace/ate-it/venv/lib/python2.7/site-packages/django/db/models/query.py", line 258, in __iter__ 
    self._fetch_all() 
    File "/Users/anubhav/workspace/ate-it/venv/lib/python2.7/site-packages/django/db/models/query.py", line 1074, in _fetch_all 
    self._result_cache = list(self.iterator()) 
    File "/Users/anubhav/workspace/ate-it/venv/lib/python2.7/site-packages/django/db/models/query.py", line 52, in __iter__ 
    results = compiler.execute_sql() 
    File "/Users/anubhav/workspace/ate-it/venv/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 848, in execute_sql 
    cursor.execute(sql, params) 
    File "/Users/anubhav/workspace/ate-it/venv/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute 
    return super(CursorDebugWrapper, self).execute(sql, params) 
    File "/Users/anubhav/workspace/ate-it/venv/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
    File "/Users/anubhav/workspace/ate-it/venv/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__ 
    six.reraise(dj_exc_type, dj_exc_value, traceback) 
    File "/Users/anubhav/workspace/ate-it/venv/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
    File "/Users/anubhav/workspace/ate-it/venv/lib/python2.7/site-packages/sql_server/pyodbc/base.py", line 538, in execute 
    return self.cursor.execute(sql, params) 
ProgrammingError: ('42S02', "[42S02] [FreeTDS][SQL Server]Invalid object name 'ate_it.restaurant'. (208) (SQLExecDirectW)") 

然而,當我手動運行使用django.db.connections查詢SELECT * from ate_it.restaurant運行良好。 我可以防止django添加方括號嗎?

我的數據庫設置

DATABASES = { 
    'default': { 
     'ENGINE': 'sql_server.pyodbc', 
     'NAME': 'mydatabase', 
     'USER' : 'myuser', 
     'PASSWORD' : 'mypwd', 
     'HOST' : 'myserver', 
     'PORT' : '1433', 
    } 
} 

我跟着this link爲freetds的和unixODBC的安裝

+0

?有幾個;我猜pyodbc == 3.0.10,但哪個Django包? 'ate_it.restaurant'是模式+表名組合嗎? – FlipperPA

+0

我正在使用Django 1.9.7。 ate_it是架構,餐廳是表名 –

回答

1

這是因爲pyodbc的。它生成表格,索引或列的引用名稱。 修改此功能quote_name在pyodbc/operations.py 改變這一行
return '[%s]' % name到您使用了Python和Django的什麼包和版本return name