Why do I get "table not found" even though I can see it created?
Question: I first created the User model before I read the Django documentation about
authentication so I put all attributes in the same model. So, later I tried to
split it into User and User profile. But when I run the the population script,
it says User profile table is not found even though I saw the SQL that created
it.

These are two classes connected to the User model that I import.

    
    
    from django.contrib.auth.models import User
    
    class UserProfile(models.Model):
            user = models.OneToOneField(User)
            profilepic = models.ImageField(blank=True)
            city = models.ForeignKey(City)
            slug = models.SlugField(unique=True)
    
            def save(self, *args, **kwargs):
                @property
                def avg_rating(self):
                    return self.userrating_set.all().aggregate(Avg('rating'))['rating__avg']
    
        class UserRating(models.Model):
            user = models.ForeignKey(User)
            comment = models.CharField(max_length=500)
            for_username = models.CharField(max_length=128)
            rating = models.IntegerField(default=5)
    
            def __unicode__(self):
                return unicode(self.rating)
    

And this is the portion of the population script where the problem is:

    
    
    new_user = User.objects.get_or_create(username=username, email=email)[0]
    #new_user.profilepic = profile_picture
    new_user.firstname = first_name
    new_user.secondname = last_name
    new_user.save()
    
    new_user_profile = UserProfile.objects.get_or_create(user=new_user, city=created_city)
    new_user_profile.slug = username
    new_user_profile.save()
    

And this is the error I get when running the script:

    
    
    Traceback (most recent call last):
      File "C:\Users\bnbih\excurj\populationScript.py", line 108, in <module>
        populate()
      File "C:\Users\bnbih\excurj\populationScript.py", line 101, in populate
        new_user_profile = UserProfile.objects.get_or_create()
      File "C:\python27\lib\site-packages\django\db\models\manager.py", line 92, in manager_method
        return getattr(self.get_queryset(), name)(*args, **kwargs)
      File "C:\python27\lib\site-packages\django\db\models\query.py", line 422, in get_or_create
        return self.get(**lookup), False
      File "C:\python27\lib\site-packages\django\db\models\query.py", line 351, in get
        num = len(clone)
      File "C:\python27\lib\site-packages\django\db\models\query.py", line 122, in __len__
        self._fetch_all()
      File "C:\python27\lib\site-packages\django\db\models\query.py", line 966, in _fetch_all
        self._result_cache = list(self.iterator())
      File "C:\python27\lib\site-packages\django\db\models\query.py", line 265, in iterator
        for row in compiler.results_iter():
      File "C:\python27\lib\site-packages\django\db\models\sql\compiler.py", line 700, in results_iter
        for rows in self.execute_sql(MULTI):
      File "C:\python27\lib\site-packages\django\db\models\sql\compiler.py", line 786, in execute_sql
        [Finished in 0.8s with exit code 1]cursor.execute(sql, params)
      File "C:\python27\lib\site-packages\django\db\backends\utils.py", line 81, in execute
        return super(CursorDebugWrapper, self).execute(sql, params)
      File "C:\python27\lib\site-packages\django\db\backends\utils.py", line 65, in execute
        return self.cursor.execute(sql, params)
      File "C:\python27\lib\site-packages\django\db\utils.py", line 94, in __exit__
        six.reraise(dj_exc_type, dj_exc_value, traceback)
      File "C:\python27\lib\site-packages\django\db\backends\utils.py", line 65, in execute
        return self.cursor.execute(sql, params)
      File "C:\python27\lib\site-packages\django\db\backends\sqlite3\base.py", line 485, in execute
        return Database.Cursor.execute(self, query, params)
    django.db.utils.OperationalError: no such table: mainapp_userprofile
    


Answer: Django's [`sqlmigrate`](https://docs.djangoproject.com/en/1.9/ref/django-
admin/#sqlmigrate) just shows you _what_ will get run, it doesn't apply any
changes, you need to run
[`migrate`](https://docs.djangoproject.com/en/1.9/ref/django-admin/#migrate)

> Prints the SQL for the named migration. This requires an active database
> connection, which it will use to resolve constraint names; this means you
> must generate the SQL against a copy of the database you wish to later apply
> it on.

