我有一个模型,其中包含两个独特的字段:
class Servers(models.Model):
server_id = models.IntegerField(db_column='server_id')
nodename = models.CharField(max_length=40)
tag_name = models.CharField(max_length=180)
customer = models.ForeignKey(Customers, on_delete=models.CASCADE)
os = models.ForeignKey(Operatingsystems, on_delete=models.CASCADE)
active = models.IntegerField()
class Meta:
managed = False
db_table = 'servers'
unique_together = (('server_id', 'nodename'),)
当我运行以下查询时:
>>> Servers.objects.all()
我收到以下错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/rlthompson/.virtualenvs/venv/lib/python3.5/site-packages/django/db/models/query.py", line 232, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/Users/rlthompson/.virtualenvs/venv/lib/python3.5/site-packages/django/db/models/query.py", line 256, in __iter__
self._fetch_all()
File "/Users/rlthompson/.virtualenvs/venv/lib/python3.5/site-packages/django/db/models/query.py", line 1085, in _fetch_all
self._result_cache = list(self.iterator())
File "/Users/rlthompson/.virtualenvs/venv/lib/python3.5/site-packages/django/db/models/query.py", line 54, in __iter__
results = compiler.execute_sql()
File "/Users/rlthompson/.virtualenvs/venv/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 835, in execute_sql
cursor.execute(sql, params)
File "/Users/rlthompson/.virtualenvs/venv/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/rlthompson/.virtualenvs/venv/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/rlthompson/.virtualenvs/venv/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/rlthompson/.virtualenvs/venv/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/rlthompson/.virtualenvs/venv/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/rlthompson/.virtualenvs/venv/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 112, in execute
return self.cursor.execute(query, args)
File "/Users/rlthompson/.virtualenvs/venv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 226, in execute
self.errorhandler(self, exc, value)
File "/Users/rlthompson/.virtualenvs/venv/lib/python3.5/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorvalue
File "/Users/rlthompson/.virtualenvs/venv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 217, in execute
res = self._query(query)
File "/Users/rlthompson/.virtualenvs/venv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 378, in _query
rowcount = self._do_query(q)
File "/Users/rlthompson/.virtualenvs/venv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 341, in _do_query
db.query(q)
File "/Users/rlthompson/.virtualenvs/venv/lib/python3.5/site-packages/MySQLdb/connections.py", line 280, in query
_mysql.connection.query(self, query)
django.db.utils.OperationalError: (1054, "Unknown column 'servers.id' in 'field list'")
我在数据库上运行makemigration
和migrate
,错误仍然存在。所以我的问题是如何使用.all()
元集对数据库进行unique_together
查询?
答案 0 :(得分:2)
您已将managed
设置为False
。因此,Django没有在表上创建默认主键(id
)。
当您稍后尝试查询它时,Django ORM会查找id
并失败。
您需要至少拥有一个主键。因此,创建一个新字段或在现有字段上设置primary_key=True
。