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 migrate命令将给出错误:
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;
答案 0 :(得分:2)
尝试分两步添加字段:
考虑为此问题打开一张django票。