0

Edit:Django version 1.6.1

I keep getting this error when trying to create a RedditPost object through the admin:

no such column: subreddit_id

The subreddit_id error references RedditPost.subreddit (not Subreddit.subreddit) and then adds an _id at the end for some reason.

There's no problem with the reference. When trying to create a RedditPost from admin, the drop down menu for Subreddits shows all the available Subreddits objects.

class Subreddit(models.Model):
    subreddit     = models.CharField(max_length=100, primary_key=True)
    title         = models.CharField(max_length=100, null=False)

    def __unicode__(self):
        return smart_unicode(self.subreddit)

class RedditPost(models.Model):
    comments_link    = models.CharField(max_length=256, primary_key=True)
    submitted_link   = models.CharField(max_length=256, null=False)
    rank             = models.IntegerField(null=False)
    title            = models.CharField(max_length=100, null=False)
    reddit_timestamp = models.DateTimeField(null=False)
    updated_at       = models.DateTimeField(auto_now_add=True, auto_now=True)
    subreddit        = models.ForeignKey('Subreddit')

RESOLVED/SOLUTION: I ended using "flush" which didn't clear up the databases when I made changes (I wasn't using any migrations). I had to use:

    python manage.py sqlclear "app_name" | python manage.py dbshell

To completely clear the database and then I had to follow this link(Django South error with initial migration) to do the migration correctly.

Community
  • 1
  • 1
Alds
  • 65
  • 2
  • 8
  • django version ? did you added `RedditPost.subreddit` in a second step ? – sax Dec 20 '14 at 08:49
  • 1
    @Alds I know that style of indented code seems very neat but PEP-8 recommends against it – jamylak Dec 20 '14 at 09:46
  • so you added the `models.ForeignKey('Subreddit')` field to the `RedditPost` model but you haven't done a schema migration on the db? – Anentropic Dec 20 '14 at 10:43

2 Answers2

5

_id is automatically added by Django. there are reasions for it.

your db table RedditPost doesnot just have this column whatsoever..

after changing the table (by adding this new column), do:

if django version < 1.7: (south needs to be installed)

  python manage.py schemamigration yourapp --auto 
  python manage.py migrate yourapp

if django version >= 1.7:

  python manage.py makemigrations 
  python manage.py migrate 
doniyor
  • 36,596
  • 57
  • 175
  • 260
  • I thought that setting comment_link to primary key would make the table reference that column when it looks for an _id in RedditPost? And the it actually seems to be looking for the _id of the foreign key of Reddit Post. The error shows up as: "no such column: gallery_redditpost.subreddit_id" – Alds Dec 20 '14 at 19:10
1

I had the same problem. Delete your db.sqlite3 file. Then on the terminal,

python3 manage.py makemigrations
python3 manage.py migrate

That's it. But your all saved database values will be deleted.

bestestefan
  • 852
  • 6
  • 20