Django migration not working with postgresql? - python

I have models :
class Keyword(models.Model):
keyword_name = models.CharField(max_length=40)
keyword_category = models.ForeignKey(Category, null=True)
class Category(models.Model):
category_name = models.CharField(max_length=40)
active = models.BooleanField(default=False)
But when I create migrate this model it give error
django.db.utils.ProgrammingError: relation "main_Keyword" does not
exist
I have try many ways but it's not working.Firstly this project i'm create on local using sqlite database but when I migrate database to postgresql then it give this error. what is my mistake?
EDIT :
Request Method: GET Request
URL: http://127.0.0.1:8000/admin/main/keyword/ Django Version: 1.9.6
Exception Type: ProgrammingError Exception Value: column
main_keyword.keyword_category_id does not exist LINE 1:
...ain_keyword"."id", "main_keyword"."keyword_name", "main_keyw...
^

IMO, from the error message, the models seem to be registered in the admin.py.
Delete the code in admin.py and try the migration again.

Related

Python Django OperationalError no such column model.id

When trying to access my model via the Admin webpage using Django, I get an Operational Error stating "no such column: model_name.id"
I did my migrations and all but I am still getting the same error.
I assume I am getting this error because I had to change the name of my model. Since, I had to delete my database and migrations and had to re migrate all of my changes.
I originally got the error of not having a table built so I used some SQL to build the table and now I am stuck with a missing column under the name "id". Thanks in advance.
My models:
class PersonalUse(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
phone_number = models.IntegerField()
verification = models.IntegerField()
messages = models.BooleanField(default=False)
message_sent = models.BooleanField(default=False)

How do I migrate changes in Django when I'm uninheriting a model?

If I have two models and one inherits from another and setup the database with migrate etc. like so:
class TemplateProduct(models.Model):
type = models.CharField(max_length=20)
class Product(TemplateProduct):
name = models.CharField(max_length=80)
Then how would I migrate the db to make it so that Product does not inherit from TemplateProduct? Say I just want these models instead:
class TemplateProduct(models.Model):
type = models.CharField(max_length=20)
class Product(models.Model):
name = models.CharField(max_length=80)
When I try to migrate this, I get the following error:
django.db.utils.ProgrammingError: column "templateproduct_ptr_id" of relation "product_product" does not exist
And then when I remove the delete "templateproduct_ptr_id" from the migration, I get the following error:
django.core.exceptions.FieldError: Local field u'id' in class 'Product' clashes with field of similar name from base class 'TemplateProduct'
As the title says: how do I migrate changes in Django when I'm uninheriting a model?
So my solution was to delete both models. Then python manage.py makemigrations --merge, then I added the models back the way I wanted them, finally running python manage.py makemigrations --merge again to add the models back in.
This may not be the most elegant solution but it worked.

Django - Can we erase migration folder?

I have a class like this (I didn't know about abstract class at the beginning):
class Client(models.Model):
company_name = models.CharField(max_length=50)
referrer = models.ForeignKey(User, null=True, blank=True)
address = models.CharField(max_length=400, blank=True)
class Customer(Client):
def __str__(self):
return "{company:s}".format(company=self.company_name)
I tried to add abstract class to client, did the makemigrations but migrate crash with this message:
django.core.exceptions.FieldError: Local field 'address' in class 'Customer' clashes with field of similar name from base class 'Client'
I tried to restart from scratch because I don't need migration right now and delete the folder.
I run manage.py migrate and It tells me that I didn't have auth_user table. Then I use manage.py migrate auth then manage.py migrate and it works !
Cool, almost, my django project is now running but when I launch the test I still have the issue:
can't find auth_user table...
I guess test didn't create the migrate auth for the test database.
What did I do wrong?

How to add new columns to django userprofile model

I have added one extra custom field phone to Django user authentication. Till then it worked properly. Later I added one more custom field called permissions. After that I am facing an error message when I try to access the admin page. Here is my model.py code
from django.contrib.auth.models import User
from django.db import models
class UserProfile(models.Model):
# This line is required. Links UserProfile to a User model instance.
user = models.OneToOneField(User)
class Meta:
permissions = (
("ssc_1","can access ssc locked questions"),
)
# The additional attributes we wish to include.
phone = models.CharField(max_length =20)
permission = models.IntegerField()
# Override the __unicode__() method to return out something meaningful!
def __unicode__(self):
return self.user.username
I have followed this stackoverflow answer, when I issue python manage.py migrate command, I am getting this error message
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: table "users_userprofile" already exists
Note : My Django versions is 1.6.6
Thanks
Migrations were introduced only in django 1.7. Until then south was being used to handle migrations

How to use User as foreign key in Django 1.7

I have made a model which looks like this:
from django.contrib.auth.models import User
class Commens(models.Model):
publish = models.ForeignKey(User, unique=True)
example = models.CharField(max_length=1000)
But when I run python manage.py syncdb , I get:
ValueError: Lookup failed for model referenced by field
books.Commens.publish: auth.User
Where I'm wrong and how to fix this?

Categories