Hi I am having an issue with profiles_details.id does not exist
I have created a custom profile... I dont want users to access the admin so I set this to false. On sign up a user is saved in djangos USER_AUTH models and a redirect occurs to a profile form.
My code: Models
class Details(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
name = models.CharField(max_length = 50)
surname = models.CharField(max_length = 50)
about = models.CharField(max_length = 500)
etc...
The form:
class detailsStep1(forms.ModelForm):
"""
Step 0 of Profile Creation.
"""
name = forms.CharField(widget=forms.TextInput,label="Name")
surname = forms.CharField(required=False, widget=forms.TextInput,
label="Surname")
country = forms.CharField(widget=forms.TextInput,
label="Country")
state = forms.CharField(widget=forms.TextInput,
label="State/Provience")
city = forms.CharField(widget=forms.TextInput,
label="city")
statusType = forms.MultipleChoiceField(required=True,
widget=forms.CheckboxSelectMultiple,
choices=Details.STATUS_TYPE_CHOICES)
class Meta:
model = Details
fields = ['name', 'surname', 'country',
'state','city','statusType']
The View:
from django.shortcuts import render
from django.shortcuts import render_to_response, redirect
from django.template import RequestContext
from profiles.forms import detailsStep1
def steps1 (request):
if request.method == 'POST':
form = detailsStep1(data=request.POST)
if form.is_valid():
user = form.save()
return redirect('/admin/')
else:
form = detailsStep1()
return render_to_response('profiles/step1.html', {
'form': form,
}, context_instance=RequestContext(request))
Below is the error message and trace
ProgrammingError at /profiles/steps1
column profiles_details.id does not exist
LINE 1: ...25:15.417979+00:00'::timestamptz, 0, 0) RETURNING "profiles_...
^
Request Method: POST
Request URL: http://127.0.0.1:8000/profiles/steps1
Django Version: 1.8.5
Exception Type: ProgrammingError
Exception Value:
column profiles_details.id does not exist
LINE 1: ...25:15.417979+00:00'::timestamptz, 0, 0) RETURNING "profiles_...
^
Exception Location: /home/g4/django/envRU/lib/python3.4/site- packages/django/db/backends/utils.py in execute, line 64
Python Executable: /home/g4/django/envRU/bin/python
Python Version: 3.4.3
Python Path:
['/home/g4/django/ru/ruV03/rulinked',
'/home/g4/django/envRU/lib/python3.4',
'/home/g4/django/envRU/lib/python3.4/plat-x86_64-linux-gnu',
'/home/g4/django/envRU/lib/python3.4/lib-dynload',
'/usr/lib/python3.4',
'/usr/lib/python3.4/plat-x86_64-linux-gnu',
'/home/g4/django/envRU/lib/python3.4/site-packages']
Server time: Mon, 26 Oct 2015 23:25:15 +0000
Any points would be great. I have run migrations and even tried a new database and deleted cache and pyc files in the project. I have no created a signal for the profile form as I assume it would only be needed in binding profile data for the admin area (still quite new to django)
## Answer
The error you're seeing, ProgrammingError: column profiles_details.id does not exist
, suggests that Django is attempting to insert a record into the Details
model (which is related to your profiles_details
table), but the id
field, which is automatically created by Django as the primary key, doesn't exist in the database. This is most likely happening because the table structure hasn't been properly updated, even though you've run migrations.
Here are some potential causes and solutions to the issue:
1. Missing Migrations
Even though you've run migrations, the id
field is automatically created as the primary key for a model unless you specify otherwise. If the database schema doesn't match the Django models, it usually indicates that migrations are either missing or weren't applied correctly.
Solution:
-
First, check the status of your migrations using:
python manage.py showmigrations
-
If there are unapplied migrations for your app, apply them:
python manage.py migrate
-
If you have recently added or changed the
Details
model and haven't created migrations, create a migration for your changes:python manage.py makemigrations
-
Then apply the migrations:
python manage.py migrate
2. Inspect the Database Schema
Ensure that the id
column exists in the profiles_details
table. If you're using a database management tool like psql
(for PostgreSQL), sqlite3
, or MySQL Workbench, you can directly inspect the table schema.
For example, for SQLite:
sqlite3 db.sqlite3
Then run:
PRAGMA table_info(profiles_details);
Check if the id
column is listed as the primary key. If not, your migrations might not have been applied correctly, or there may be an issue with the initial table creation.
3. Custom Primary Key or Model Changes
If you explicitly defined a custom primary key in the Details
model (e.g., using primary_key=True
on another field), ensure that the id
field is still present, or you have configured a different field as the primary key.
For example, if you have:
class Details(models.Model):
custom_id = models.CharField(max_length=50, primary_key=True)
# other fields
This would prevent Django from creating the id
field, and you'd need to use custom_id
as the primary key.
If this is the case, ensure that any database queries (like the one causing the error) refer to the correct primary key field.
4. Database Corruption / Cache Issues
If you've run migrations and created new tables but are still seeing the error, it's possible that your database schema is out of sync, or there may be a corrupted migration.
Solution:
-
Delete Migrations and Database (If in Development Mode): If you're working in development and don't have critical data, you can delete the database and migrations folder, then regenerate everything.
- Delete the
db.sqlite3
file or your database file. - Delete the
migrations/
folder in your app (except for the__init__.py
file). - Run:
python manage.py makemigrations python manage.py migrate
- Delete the
-
Inspect Migration Files: Sometimes, if migrations are not properly generated, the database schema might not be updated. You can inspect the generated migration files in the
migrations/
folder and check if they properly reflect the model changes, especially the addition of theid
field.
5. Issue with Form Saving
In your view, you're calling form.save()
, but you're not specifying how to link the newly created Details
object to the user. This could be another source of the problem.
Solution:
Modify your steps1
view to save the form data correctly, linking the newly created Details
object to the user. You can do this by setting the user
field on the form before saving it.
from django.shortcuts import render, redirect
from profiles.forms import detailsStep1
from django.contrib.auth.decorators import login_required
@login_required
def steps1(request):
if request.method == 'POST':
form = detailsStep1(data=request.POST)
if form.is_valid():
# Set the user field before saving the form
details = form.save(commit=False)
details.user = request.user # Associate the Details with the logged-in user
details.save() # Now save the form
return redirect('/some-other-page/') # Redirect to a different page after form save
else:
form = detailsStep1()
return render(request, 'profiles/step1.html', {'form': form})
6. Admin Access
Although you're not using the Django admin panel, I see you're using redirect('/admin/')
. This might lead to an invalid state if it's not handled correctly. Ensure that after saving the profile details, you redirect to a proper page in your application, not the admin panel unless it's part of your logic.
Summary of Steps:
- Check the status of migrations (
python manage.py showmigrations
). - Ensure all migrations have been applied (
python manage.py migrate
). - Verify that the
id
column exists in the database table. - If using a custom primary key, check the model definition.
- Update your view to link the user to the
Details
object on save.
Let me know if these steps resolve your issue or if you need further assistance!