hi i am working on a django python application using sqlite3 database. I have a an extention to django's user model defined in my models.py as follows:
#Account Model
class Account(models.Model):
user = models.OneToOneField(User)
avatar_url = models.CharField(max_length=200)
profile_url = models.CharField(max_length=200)
account_type = models.CharField(max_length=60, choices=choices.ACCOUNT_TYPE)
i also have a method to create the Account object
and a post_save
handler defined like this:
#Function to Create user Account/Profile
def create_user_account(sender, instance, created, **kwargs):
if created:
models.Account.objects.create(user=instance)
#Create User / User Registration
def UserRegistration(request):
if request.method == 'POST':
username = request.POST['fn'].capitalize() + ' ' + request.POST['ln'].capitalize()
# CREATE USER
newuser = User.objects.create_user(username=username,
email=request.POST['email'],
password=request.POST['pw'])
newuser.save()
return HttpResponse(username)
#Post Save handler to create user Account/Profile
post_save.connect(create_user_account, sender=User)
Now, whenever i try to register a new user i get the following database error:
DatabaseError at /register/
table engine_account has no column named user_id
Request Method: POST
Request URL: http://localhost:8000/register/
Django Version: 1.4
Exception Type: DatabaseError
Exception Value:
table engine_account has no column named user_id
Exception Location: /usr/local/lib/python2.7/dist-packages/Django-1.4-py2.7.egg/django/db/backends/sqlite3/base.py in execute, line 337
Python Executable: /usr/bin/python
Python Version: 2.7.3
and i have no idea where that "user_id"
field is coming from .. any ideas ?
PS:
table engine_account is basically the Account
class in the application named Engine
Did you edit models.py after you ran syncdb?
If so,then you should manually edit your table or recreate your database using:
python manage.py syncdb
in project directory to apply the changes.
Update 2019 : syncdb
is deprecated in Django 1.9 onwards. Use
python manage.py makemigrations <app_name>
python manage.py migrate --run-syncdb
Update 2020 : I would like to add more description to my answer.
This problem occurs if the model and the corresponding table does not match. This means you have altered your model but not migrated your changes. Some do not get the difference between makemigrations
and migrate
commands. To make it more clear, I will try to explain the difference:
./manage.py makgemigrations <app_name>
: The changes on the models of given app will compared with the previous migrations. If no app_name
is provided all of the tracked apps will be controlled and if has any changes the migrations will be created. Each app has its migrations directory and this command will create the related migrations inside this directory. When a migration is created it has no changes on the DB, the migrations needs to be applied. If you do not apply your migrations then you will still get the same error.
./manage.py migrate
: The migrations created by makemigrations
command is applied to DB. You need to be careful while you are migrating on the production environment.
./manage.py showmigrations
: This command lists the statuses of the migrations, you can see if a migration is applied or not. Normally the statuses are stored in the DB, but there is no need to connect to db and crawl the data. This command gives you a clear input.
syncdb
is deprecated in django 1.9 and later versions. So for django versions 1.9 or later run python manage.py makemigrations <app-name>
and then python manage.py migrate
I tried python manage.py syncdb
, that wasn't enough, so the solution was :
I deleted
migrations file
and I deleteddb.sqlite3
file,
I executed
python manage.py makemigrations
and alsopython manage.py migrate
Bingo !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With