Reputation: 379
I have the following code in my models.py file:
from django.db import models
from django.db.models import permalink
class Page(models.Model):
name = models.CharField(max_length=100,db_index=True, unique=True)
slug = models.SlugField(max_length=100,db_index=True, unique=True)
def __unicode__(self):
return '%s' % self.name
class Category(models.Model):
name = models.CharField(max_length=100, db_index=True, unique=True)
slug = models.SlugField(max_length=100, db_index=True, unique=True)
def __unicode__(self):
return '%s' % self.name
class Post(models.Model):
title = models.CharField(max_length=100, unique=True, db_index=True)
body = models.TextField()
post_date = models.DateField(db_index = True, auto_now_add=True)
category = models.ForeignKey('board.Category')
page = models.ForeignKey('board.Page')
def __unicode__(self):
return '%s' % self.title
@permalink
def get_absolute_url(self):
return "/%i/%i" % Page.slug, self.id
When I try to add a Post object in the admin page (I configured the admin.py too), I get the following error:
no such column: board_category.name
I did my research and tried dropping the table and doing syncdb again but somehow it still shows this error.
So I listed my tables:
CREATE TABLE "board_page" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(100) NOT NULL UNIQUE,
"slug" varchar(100) NOT NULL UNIQUE
)
;
CREATE TABLE "board_category" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(100) NOT NULL UNIQUE,
"slug" varchar(100) NOT NULL UNIQUE
)
;
CREATE TABLE "board_post" (
"id" integer NOT NULL PRIMARY KEY,
"title" varchar(100) NOT NULL UNIQUE,
"body" text NOT NULL,
"post_date" date NOT NULL,
"category_id" integer NOT NULL REFERENCES "board_category" ("id"),
"page_id" integer NOT NULL REFERENCES "board_page" ("id")
)
;
CREATE INDEX "board_post_896eb94b" ON "board_post" ("post_date");
CREATE INDEX "board_post_6f33f001" ON "board_post" ("category_id");
CREATE INDEX "board_post_3fb9c94f" ON "board_post" ("page_id");
I'm not exactly an expert in SQL but I can clearly see that the board_page.name column exists so I really have no idea why this is giving me an error.
And the strangest thing is that adding another page or category actually works....
--EDIT
from django.contrib import admin
from board.models import Page, Post, Category
class PageAdmin(admin.ModelAdmin):
#fields = ['name','slug']
pass
class PostAdmin(admin.ModelAdmin):
#fields = ['title','body','category','page']
pass
class CategoryAdmin(admin.ModelAdmin):
#fields = ['name','slug']
pass
admin.site.register(Page, PageAdmin)
admin.site.register(Post, PostAdmin)
admin.site.register(Category, CategoryAdmin)
Upvotes: 2
Views: 5347
Reputation: 1258
If you add something to your models and after that you did not sync, this happens. If you are in beginning for your project, I recommend to remove your db and recreate it and then:
python manage.py syncdb
Otherwise use South. http://south.readthedocs.org/en/latest/
It's interesting but sometimes rebooting the server helps. It worked for me a few times in past.
Upvotes: 1
Reputation: 37934
syncdb
will only create tables for models which have not yet been installed. It will never issue ALTER TABLE statements to match changes made to a model class after installation.
better use South to migrate the changes of your models.
first migration with:
python manage.py schemamigration yourapp --initial
python manage.py migrate yourapp
other migrations:
python manage.py schemamigration yourapp --auto
python manage.py migrate yourapp
Upvotes: 2