gpt4 book ai didi

mysql - 错误 1054, "Unknown column ' 中的 'field list' student_info.id' - Django

转载 作者:可可西里 更新时间:2023-11-01 07:48:20 41 4
gpt4 key购买 nike

我有一个现有的数据库,其中包含一个包含 5 列和大量数据的特定表。 (我从 CSV 导入这个)

如果我在 MySql 中执行 DESCRIBE student_info,它会显示:

+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| pic_id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| hostel | varchar(3) | YES | | NULL | |
| room | int(11) | YES | | NULL | |
| branch | varchar(4) | YES | | NULL | |
| id_no | varchar(12) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+

这个表包含所有数据,正如我通过各种 SELECT 语句和通过 Workbench 测试的那样

在 settings.py 中,我配置了我的项目以成功连接到该数据库。然后,我下达了命令

python manage.py inspectdb > studentinfo/models.py

models.py 的内容变成了:

class StudentInfo(models.Model):
pic_id = models.IntegerField(null=True, blank=True)
name = models.CharField(max_length=150, blank=True)
hostel = models.CharField(max_length=9, blank=True)
room = models.IntegerField(null=True, blank=True)
branch = models.CharField(max_length=12, blank=True)
id_no = models.CharField(max_length=36, blank=True)
class Meta:
db_table = u'student_info'

然后,如果我在 syncdb 之后执行:

python manage.py sqlall studentinfo
BEGIN;
CREATE TABLE `student_info` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`pic_id` integer,
`name` varchar(150) NOT NULL,
`hostel` varchar(9) NOT NULL,
`room` integer,
`branch` varchar(12) NOT NULL,
`id_no` varchar(36) NOT NULL
)
;
COMMIT;

所以一切似乎都井井有条,除了如果我试图检索任何东西我会收到这个错误:

>>> from studentinfo.models import StudentInfo
>>> list=StudentInfo.objects.all()
>>> list
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/django/db/models/query.py", line 69, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/usr/lib/python2.7/dist-packages/django/db/models/query.py", line 84, in __len__
self._result_cache.extend(self._iter)
File "/usr/lib/python2.7/dist-packages/django/db/models/query.py", line 273, in iterator
for row in compiler.results_iter():
File "/usr/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 680, in results_iter
for rows in self.execute_sql(MULTI):
File "/usr/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 735, in execute_sql
cursor.execute(sql, params)
File "/usr/lib/python2.7/dist-packages/django/db/backends/util.py", line 34, in execute
return self.cursor.execute(sql, params)
File "/usr/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 86, in execute
return self.cursor.execute(query, args)
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
**OperationalError: (1054, "Unknown column 'student_info.id' in 'field list'")**

知道是什么原因造成的吗?

最佳答案

sqlall 仅显示如果您使用空数据库运行 syncdb 将创建什么。从您的 DESCRIBE 可以明显看出,没有 id 字段 - 实际上根本没有主键,这是一个非常奇怪的设计。您将需要通过 SQL 手动添加该字段。

关于mysql - 错误 1054, "Unknown column ' 中的 'field list' student_info.id' - Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11247422/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com