I am very new to django and was able to finish the tutorial on djangoproject.com without any errors. I am now going through the Django REST framework tutorial found at http:
Let's focus on the error:
Exception Value: no such column: snippets_snippet.owner_id
Let's see if that's true...
You can use the manage.py command to access your db shell (this will use the settings.py variables, so you're sure to connect to the right one).
manage.py dbshell
You can now show the details of your table by typing:
.schema TABLE_NAME
Or in your case:
.schema snippets_snippet
More sqlite commands can be found here or by issuing a:
.help
Lastly, end your session by typing:
.quit
This doesn't get you out of the woods, but it helps you know what end of the problem to work on :)
Good luck!
As you went through the tutorial you must have come across the section on migration, as this was one of the major changes in Django 1.7
Prior to Django 1.7, the syncdb command never made any change that had a chance to destroy data currently in the database. This meant that if you did syncdb for a model, then added a new row to the model (a new column, effectively), syncdb would not affect that change in the database.
So either you dropped that table by hand and then ran syncdb again (to recreate it from scratch, losing any data), or you manually entered the correct statements at the database to add only that column.
Then a project came along called south
which implemented migrations. This meant that there was a way to migrate forward (and reverse, undo) any changes to the database and preserve the integrity of data.
In Django 1.7, the functionality of south
was integrated directly into Django. When working with migrations, the process is a bit different.
models.py
(as normal).makemigrations
command. This command is smart enough to detect what has changed and will create a script to effect that change to your database.migrate
. This command applies all migrations in order.So your normal syncdb
is now a two-step process, python manage.py makemigrations
followed by python manage.py migrate
.
Now, on to your specific problem:
class Snippet(models.Model):
owner = models.ForeignKey('auth.User', related_name='snippets')
highlighted = models.TextField()
created = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=100, blank=True, default='')
code = models.TextField()
linenos = models.BooleanField(default=False)
language = models.CharField(choices=LANGUAGE_CHOICES,
default='python',
max_length=100)
style = models.CharField(choices=STYLE_CHOICES,
default='friendly',
max_length=100)
In this model, you have two fields highlighted
and code
that is required (they cannot be null).
Had you added these fields from the start, there wouldn't be a problem because the table has no existing rows?
However, if the table has already been created and you add a field that cannot be null, you have to define a default value to provide for any existing rows - otherwise, the database will not accept your changes because they would violate the data integrity constraints.
This is what the command is prompting you about. You can tell Django to apply a default during migration, or you can give it a "blank" default highlighted = models.TextField(default='')
in the model itself.
Initially ,I have commented my new fields which is causing those errors, and run python manage.py makemigrations and then python manage.py migrate to actually delete those new fields.
class FootballScore(models.Model):
team = models.ForeignKey(Team, related_name='teams_football', on_delete=models.CASCADE)
# match_played = models.IntegerField(default='0')
# lose = models.IntegerField(default='0')
win = models.IntegerField(default='0')
# points = models.IntegerField(default='0')
class FootballScore(models.Model):
team = models.ForeignKey(Team, related_name='teams_football', on_delete=models.CASCADE)
match_played = models.IntegerField(default='0')
lose = models.IntegerField(default='0')
win = models.IntegerField(default='0')
points = models.IntegerField(default='0')
Then i freshly uncommented them and run python manage.py makemigrations and python manage.py migrate and boom. It worked for me. :)
I am also faced same problem.
If you're adding a new field then it gives the error as no column found.
Then you apply make migration
command and after that migrate
command
Then still same error..
EX...
path=models.FilePathField()
Add default value for field
path=models.FilePathField(default='')
and than apply command
python manage.py makemigrations
python manage.py migrate
It may help you
I had this problem recently, even though on a different tutorial. I had the django version 2.2.3 so I thought I should not have this kind of issue.
In my case, once I add a new field to a model and try to access it in admin
, it would say no such column
.
I learnt the 'right' way after three days of searching for solution with nothing working.
First, if you are making a change to a model, you should make sure that the server is not running. This is what caused my own problem.
And this is not easy to rectify. I had to rename the field (while server was not running) and re-apply migrations.
Second, I found that python manage.py makemigrations <app_name>
captured the change as opposed to just python manage.py makemigrations
. I don't know why.
You could also follow that up with python manage.py migrate <app_name>
. I'm glad I found this out by myself.
If the error persists after doing what @Burhan Khalid said
Try this line: python manage.py migrate --run-syncdb