0

I'm deploying my django app with Dotcloud. I use Postgres as DB.

I had a new model to my app, and I wanted to flush and syncdb the db. Everything works find when I do it. My new model, named 'Competition' appears in my admin.

The problem is that an other model, Match, has a ForeignKey with the model Competition. And when I go to 'Matchs' in my admin, I get this error:

     DatabaseError at /admin/myproject/match/
     column myproject_match.competition_id does not exist
     LINE 1: ...team_challenger_id", "sportdub_match"."sport_id", "sportdub_...

Any idea on why the syncdb didn't make it work find?

Thank you for your help.

EDIT: My two models

     class Competition(models.Model):
       name = models.CharField(max_length=256)
       comp_pic = models.ImageField(upload_to="comp_pics/")

       def __unicode__(self):
           return self.name


     class Match(models.Model):
        team_host = models.ForeignKey(Team, related_name='host_matches')
        team_challenger = models.ForeignKey(Team, related_name= 'challenger_matches')
        sport = models.ForeignKey(Sport)
        competition = models.ForeignKey(Competition)
Juliette Dupuis
  • 1,027
  • 2
  • 13
  • 22
  • Can you post your two model source code? Are you sure you ran syncdb and the column/table is in your database? Have you logged into the database to confirm? – Ken Cochrane Nov 28 '12 at 18:48
  • I edited my question. Yes I ran syncdb. And I think dotcloud does that automatically when I flush it. I don't know if the column/table are in my database. How can I check that? – Juliette Dupuis Nov 28 '12 at 18:55
  • And the change in my models.py works perfectly locally – Juliette Dupuis Nov 28 '12 at 18:56
  • dotCloud doesn't run syncdb automatically, you need to explicitly run it, or put it in your postinstall script. You can log into your database directly by sshing into your database service and running the psql command, or using a GUI database browser like pgadmin or navicat. – Ken Cochrane Nov 28 '12 at 19:50

1 Answers1

1

manage.py syncdb will only create missing tables. If a table already exists, but with an invalid definition, it will not be updated. This is very probably the problem that you are experiencing.

There are at least three ways to solve the problem.

  1. The easy way: use manage.py reset to effectively drop+recreate all the tables. Easy, but of course you will lose the data.
  2. The lazy way: install django_extensions and use manage.py sqldiff. It will show you the difference between the current structure of the database, and the expected structure. sqldiff will nicely show you SQL statements which can update the existing database to conform with your models. You can then execute those statements in a SQL shell.
  3. The clean way: use a migrations framework like south to handle database schema updates. Of course, if you are early in the development, this is overkill (you do not want to write database migrations each time you add/change/remove a field while you're doing local development!) but if your project will have a longer life span, I definitely recommend checking out south.
jpetazzo
  • 14,874
  • 3
  • 43
  • 45
  • THANK YOU! The first solution works perfectly. The only curious thing is that after reset, I wasn't asked to create a superuser, the old one has been kept. So I had an error 'UserProfile matching query does not exist'. So I did a flush after the reset. Everything works find. thank you again – Juliette Dupuis Nov 28 '12 at 19:26