简体   繁体   中英

“No such column” error during deletion of a record in Django admin

I have following model ( models.py ) in my Django project:

class Topic(models.Model):
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=140)

    def __unicode__(self):
        return self.title

class ArgumentElement(models.Model):
    id = models.AutoField(primary_key=True)
    contents = models.CharField(max_length=256)
    elementType = models.CharField(max_length=10)
    topic = models.ForeignKey(Topic, related_name='ArgumentElement_Topic')

    def __unicode__(self):
        return self.contents

class ArgumentElementConnection(models.Model):
    id = models.AutoField(primary_key=True)
    sourceId = models.ForeignKey(ArgumentElement, related_name='connection_source')
    targetId = models.ForeignKey(ArgumentElement, related_name='connection_target')
    topic = models.ForeignKey(Topic, related_name='ArgumentElementConnection_Topic')
    connectionType = models.CharField(max_length=7)

    def __unicode__(self):
        return self.id

I add all three models to the admin ( admin.py ):

from django.contrib import admin
from history_site.opinions.models import ArgumentElement, ArgumentElementConnection, Topic

admin.site.register(ArgumentElement, admin.ModelAdmin)
admin.site.register(ArgumentElementConnection, admin.ModelAdmin)
admin.site.register(Topic, admin.ModelAdmin)

When I create an instance of Topic and then try to delete it in the Admin, I get the error no such column: opinions_argumentelement.topic_id .

What's wrong with my models.py ?

It seems that AutoFields using the sqllite3 backend don't increment properly . Is there any reason you are including the line id = models.AutoField(primary_key=True) ? If you leave it out, an auto-increment primary key field will be automatically added and is more likely to be correctly created. Try deleting that line and creating a new sqllite database file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM