当我尝试保存具有外键的模型时,我收到错误。应用名称为“音乐”。我的模特。
class Event(models.Model):
eventUrl = models.URLField('url', primary_key=True)
eventTime = models.DateTimeField('date and time')
location = models.CharField('location/venue',max_length='75')
title = models.CharField('title',max_length='255')
def __unicode__(self):
return self.eventUrl
class Performer(models.Model):
url = models.ForeignKey(Event)
performer = models.CharField('performer', max_length='75')
instrument = models.CharField('instrument or ensemble', max_length='75')
def __unicode__(self):
return self.performer
生成的表
class MusicEvent(models.Model):
eventurl = models.CharField(max_length=200, primary_key=True, db_column=u'eventUrl') # Field name made lowercase.
eventtime = models.DateTimeField(db_column=u'eventTime') # Field name made lowercase.
location = models.CharField(max_length=75)
title = models.CharField(max_length=255)
class Meta:
db_table = u'music_event'
class MusicPerformer(models.Model):
id = models.IntegerField(primary_key=True)
performer = models.CharField(max_length=75)
class Meta:
db_table = u'music_performer'
class MusicPerformerEventurl(models.Model):
id = models.IntegerField(primary_key=True)
performer_id = models.IntegerField()
event = models.ForeignKey(MusicEvent)
class Meta:
db_table = u'music_performer_eventUrl'
我视图中的代码
p = Performer(url=e,performer=name,instrument=inst)
p.save()
我收到以下错误
Request Method: POST
Request URL: http://127.0.0.1:8000/events/import_data/
Django Version: 1.3.1
Exception Type: DatabaseError
Exception Value:
table music_performer has no column named url_id
Exception Location: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py in execute, line 234
Python Executable: /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Python Version: 2.7.2
所以似乎说该表没有列url,这是外键。为什么它没有生成列作为列,即使它在我的模型中?如何将“事件”外键分配给新的执行者对象?
答案 0 :(得分:0)
我没有意识到“flush”不会丢弃表,“sync”不会改变现有的表。我通过手动删除shell中的表并再次运行“sync”来修复此问题。更改模型(添加或更改现有字段的名称?)时更新表的“标准”是什么?