1
votes

Summary: I'm following a south tutorial. I changed a models.py file, but when I run "migrate", it doesn't update the psql table. I've no idea why not.

I edited polls/models.py I added a class, "Survey" I added a line in "Question" to refer to survey

models.py

I added the class Survey

and I added this line in the class Question:

survey = models.ForeignKey(Survey)

All the rest already existed (basically as per django tutorial)

import datetime
from django.db import models
from django.utils import timezone

# Create your models here.

### Adding the Survey class just now, to practice using South
class Survey(models.Model):
  survey_name = models.CharField(max_length=190)
  pub_date = models.DateTimeField('date published')
  def __unicode__(self): #__str__ instead of __unicode__ if using Python 3, but unicode for python 2
    return self.survey_name
  def was_published_recently(self):
    return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
  was_published_recently.admin_order_field = 'pub_date'
  was_published_recently.boolean = True
  was_published_recently.short_description = 'Published recently?' # for admin site


class Question(models.Model):
  survey = models.ForeignKey(Survey) ### this is a new line based on adding the Survey class above
  question_text = models.CharField(max_length=200)
  pub_date = models.DateTimeField('date published')
  def __unicode__(self): #__str__ instead of __unicode__ if using Python 3, but unicode for python 2
    return self.question_text
  def was_published_recently(self):
    return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
  was_published_recently.admin_order_field = 'pub_date'
  was_published_recently.boolean = True
  was_published_recently.short_description = 'Published recently?' # for admin site


class Choice(models.Model):
  question = models.ForeignKey(Question)
  choice_text = models.CharField(max_length=200)
  votes = models.IntegerField(default=0)
  def __unicode__(self):
    return self.choice_text

terminal

  1. I did schemamigration, which says it added model polls.Survey

    (app01)MoriartyMacBookAir13:getstartapp macuser$ vim polls/models.py (app01)MoriartyMacBookAir13:getstartapp macuser$ python manage.py schemamigration polls --auto You cannot use --auto on an app with no migrations. Try --initial. (app01)MoriartyMacBookAir13:getstartapp macuser$ sudo python manage.py schemamigration polls --initial

    • Added model polls.Survey
    • Added model polls.Question
    • Added model polls.Choice Created 0001_initial.py. You can now apply this migration with: ./manage.py migrate polls

I double-checked the Survey doesn't exist in the psql table yet:

(app01)MoriartyMacBookAir13:getstartapp macuser$ heroku pg:psql
---> Connecting to HEROKU_POSTGRESQL_YELLOW_URL (DATABASE_URL)
psql (9.3.5, server 9.3.3)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.

app01::YELLOW=> \d
                             List of relations
 Schema |                 Name                 |   Type   |     Owner      
--------+--------------------------------------+----------+----------------
 public | account_emailaddress                 | table    | aqolirupsmmmqz
 public | account_emailaddress_id_seq          | sequence | aqolirupsmmmqz
 public | account_emailconfirmation            | table    | aqolirupsmmmqz
 public | account_emailconfirmation_id_seq     | sequence | aqolirupsmmmqz
 public | auth_group                           | table    | aqolirupsmmmqz
 public | auth_group_id_seq                    | sequence | aqolirupsmmmqz
 public | auth_group_permissions               | table    | aqolirupsmmmqz
 public | auth_group_permissions_id_seq        | sequence | aqolirupsmmmqz
 public | auth_permission                      | table    | aqolirupsmmmqz
 public | auth_permission_id_seq               | sequence | aqolirupsmmmqz
 public | auth_user                            | table    | aqolirupsmmmqz
 public | auth_user_groups                     | table    | aqolirupsmmmqz
 public | auth_user_groups_id_seq              | sequence | aqolirupsmmmqz
 public | auth_user_id_seq                     | sequence | aqolirupsmmmqz
 public | auth_user_user_permissions           | table    | aqolirupsmmmqz
 public | auth_user_user_permissions_id_seq    | sequence | aqolirupsmmmqz
 public | discover_author                      | table    | aqolirupsmmmqz
 public | discover_author_id_seq               | sequence | aqolirupsmmmqz
 public | discover_awedio                      | table    | aqolirupsmmmqz
 public | discover_awedio_id_seq               | sequence | aqolirupsmmmqz
 public | discover_user                        | table    | aqolirupsmmmqz
 public | discover_user_id_seq                 | sequence | aqolirupsmmmqz
 public | django_admin_log                     | table    | aqolirupsmmmqz
 public | django_admin_log_id_seq              | sequence | aqolirupsmmmqz
 public | django_content_type                  | table    | aqolirupsmmmqz
 public | django_content_type_id_seq           | sequence | aqolirupsmmmqz
 public | django_session                       | table    | aqolirupsmmmqz
 public | django_site                          | table    | aqolirupsmmmqz
 public | django_site_id_seq                   | sequence | aqolirupsmmmqz
 public | hello_greeting                       | table    | aqolirupsmmmqz
 public | hello_greeting_id_seq                | sequence | aqolirupsmmmqz
 public | polls_choice                         | table    | aqolirupsmmmqz
 public | polls_choice_id_seq                  | sequence | aqolirupsmmmqz
 public | polls_question                       | table    | aqolirupsmmmqz
 public | polls_question_id_seq                | sequence | aqolirupsmmmqz
 [other tables, but no more polls tables]
 public | south_migrationhistory               | table    | aqolirupsmmmqz
 public | south_migrationhistory_id_seq        | sequence | aqolirupsmmmqz
(45 rows)

app01::YELLOW=> \q
  1. Now in order to update that and create polls_survey, I run the migrate command as advised... however, the psql table is not updated. Any suggestions why? Here is the full response for the commands:

```
(app01)MoriartyMacBookAir13:getstartapp macuser$ sudo python manage.py migrate polls Running migrations for polls: - Nothing to migrate. - Loading initial data for polls. Installed 0 object(s) from 0 fixture(s) (app01)MoriartyMacBookAir13:getstartapp macuser$ sudo python manage.py syncdb Syncing... Creating tables ... Installing custom SQL ... Installing indexes ... Installed 0 object(s) from 0 fixture(s)

Synced:
 > django.contrib.admin
 > django.contrib.auth
 > django.contrib.contenttypes
 > django.contrib.sessions
 > django.contrib.messages
 > django.contrib.staticfiles
 > storages
 > evernote
 > discover
 > hello
 > south
 > django.contrib.sites
 > djrill
 > allauth.socialaccount.providers.linkedin

Not synced (use migrations):
 - polls
 - allauth
 - allauth.account
 - allauth.socialaccount
 - allauth.socialaccount.providers.facebook
(use ./manage.py migrate to migrate these)
(app01)MoriartyMacBookAir13:getstartapp macuser$ sudo python manage.py migrate polls
Running migrations for polls:
- Nothing to migrate.
 - Loading initial data for polls.
Installed 0 object(s) from 0 fixture(s)
(app01)MoriartyMacBookAir13:getstartapp macuser$ 

(app01)MoriartyMacBookAir13:getstartapp macuser$ sudo python manage.py convert_to_south polls
This application is already managed by South.
(app01)MoriartyMacBookAir13:getstartapp macuser$ sudo python manage.py migrate polls
Running migrations for polls:
- Nothing to migrate.
 - Loading initial data for polls.
Installed 0 object(s) from 0 fixture(s)
(app01)MoriartyMacBookAir13:getstartapp macuser$ sudo python manage.py schemamigration polls --auto
Nothing seems to have changed.
(app01)MoriartyMacBookAir13:getstartapp macuser$ 

```

That is, the polls_survey still doesn't exist in the psql table, yet syncdb and migrate are having no effect... so psql is not reflecting the data model that python describes...

Any suggestions what I'm doing wrong here?

In case useful: the settings.py file has my heroku psql login details in it, hence why I'm able to successfully run heroku pg:psql command above

1

1 Answers

1
votes

I think you have to run schemamigration --initial before changing the models and and adding the models. After running the schemamigration --initial then change the models and add new models. then run schemamigration --auto , and then migrate command . It should change your database accordingly.

Steps

  • Run python manage.py schemamigration polls --initial (Before changing the models)
  • Change the models.py
  • Run python manage.py schemamigration polls --auto
  • Run python manage.py migrate polls