添加外键后 Django 迁移给出 "unknown column" 错误
Django migrate after adding foreign key gives "unknown column" error
class Agent(models.Model):
full_name = models.CharField(max_length=200)
short_code = models.CharField(max_length=200)
class Phone(models.Model):
date = models.DateTimeField('Call Time')
...
// Newly added:
agent = models.ForeignKey(Agent, on_delete=models.CASCADE)
我正在使用 mysql。添加外键后,makemigrations 命令就可以运行了。 python manage.py 迁移命令会给出错误:
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 112, in execute
return self.cursor.execute(query, args)
File "/Library/Python/2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/Library/Python/2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.OperationalError: (1054, "Unknown column 'agent' in 'dash_phone'")
python manage.py sqlmigrate dash 0004 给出:
BEGIN;
--
-- Alter field agent on phone
--
ALTER TABLE `dash_phone` CHANGE `agent` `agent_id` integer NOT NULL;
ALTER TABLE `dash_phone` MODIFY `agent_id` integer NOT NULL;
CREATE INDEX `dash_phone_agent_id_e9e1c068_uniq` ON `dash_phone` (`agent_id`);
ALTER TABLE `dash_phone` ADD CONSTRAINT `dash_phone_agent_id_e9e1c068_fk_dash_agent_id` FOREIGN KEY (`agent_id`) REFERENCES `dash_agent` (`id`);
COMMIT;
嗯,这似乎是一个错误。 Django 在提交新代理字段之前将代理字段转换为外键。它以 ALTER TABLE ... CHANGE 开头,在它实际添加字段之前。
尝试分两步添加字段:
- 添加一个 IntegerField 代理字段(不是外键),makemigrations,migrate
- 将代理从整数字段更改为外键、makemigrations、migrate
考虑为这个问题开一个django ticket。
class Agent(models.Model):
full_name = models.CharField(max_length=200)
short_code = models.CharField(max_length=200)
class Phone(models.Model):
date = models.DateTimeField('Call Time')
...
// Newly added:
agent = models.ForeignKey(Agent, on_delete=models.CASCADE)
我正在使用 mysql。添加外键后,makemigrations 命令就可以运行了。 python manage.py 迁移命令会给出错误:
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 112, in execute
return self.cursor.execute(query, args)
File "/Library/Python/2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/Library/Python/2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.OperationalError: (1054, "Unknown column 'agent' in 'dash_phone'")
python manage.py sqlmigrate dash 0004 给出:
BEGIN;
--
-- Alter field agent on phone
--
ALTER TABLE `dash_phone` CHANGE `agent` `agent_id` integer NOT NULL;
ALTER TABLE `dash_phone` MODIFY `agent_id` integer NOT NULL;
CREATE INDEX `dash_phone_agent_id_e9e1c068_uniq` ON `dash_phone` (`agent_id`);
ALTER TABLE `dash_phone` ADD CONSTRAINT `dash_phone_agent_id_e9e1c068_fk_dash_agent_id` FOREIGN KEY (`agent_id`) REFERENCES `dash_agent` (`id`);
COMMIT;
嗯,这似乎是一个错误。 Django 在提交新代理字段之前将代理字段转换为外键。它以 ALTER TABLE ... CHANGE 开头,在它实际添加字段之前。
尝试分两步添加字段:
- 添加一个 IntegerField 代理字段(不是外键),makemigrations,migrate
- 将代理从整数字段更改为外键、makemigrations、migrate
考虑为这个问题开一个django ticket。