Can't add new field in the django model - django

Django 2.0
I've created a model in Blog app,
class Category(models.Model):
field1 = models....
field2 = models....
field3 = models....
and after some time I want to add a new field in that model.
class Category(models.Model):
field1 = models....
cover_pic = .models....
field2 = models....
field3 = models....
I followed this answer. But it gives me the following error.
django.db.utils.OperationalError: (1054, "Unknown column 'blog_category.cover_pic' in 'field list'")

That answer is from 2014 and for an old Django version.
The workflow for adding new fields is simple:
Write code for the field in your model.
Run command manage.py makemigrations.
Run command manage.py migrate.
This is all in the documentation.
Since, you already followed that answer and have run the manage.py --fake command, you have messed up your db state a little bit.
To recover, do this:
Go to your "blog" app's "migration" folder.
Look at the names of the migration files. They should look like 0001_category_field1.py, 0002_category_cover_pic.py.
The faked migration will be the one with name something like 0002_category_cover_pic.py. Note its number, like 0002.
Now you'll have to move back to the previously applied migration. So, if the faked migration number is 0002, the previously applied migration will be 0001.
Now run this command - manage.py migrate --fake myapp 0001. Note the number of the migration file. You'll have to fake another migration back to the previously applied migration file.
Now run command - manage.py migrate myapp.
This should fix your problem.

If you're problem is not solved yet and you don't have important data in your database, I would suggest start your database from fresh. Delete all your database tables, then delete all the migration files inside your app_name/migration folders. Now run the two commands and start developing.
python manage.py makemigrations
python manage.py migrate
Now you will have a fresh database and you are good to go. From next time try to follow the way mentioned in the above comment.

If you are adding a new field in the Django model and after deploying to any environment , you are getting error while accessing the field.
Error:
"1054, Unknown column"
Although i also was not able to figure out how to resolve it but i came up with a work around.
I created a column manually in the DB .
I added the same field in the model.
Again tried to access the field and it worked like a charm.
I hope it helps your case.

Related

change the initial value of the primary key id, and make start from it on my models

I am working on Django, so I created my table on my models.py. By default when I start registering data, the id, set as primary key, begins with the initial value 1 and increments each time I do a new register.
I have loaded some data in my table, so the final id right now is 185. I run the server and try to add a new register, but I get an error about id duplicity. Django is trying to save the register in id 1.
How do I get it to continue saving the register from id 186 and onwards?
I tried this on my models:
class MyModel(models.Model):
id = models.AutoField(primary_key=True, initial=123)
but it does not recognize the kwarg initial.
You will likely want to use sqlsequencereset.
You can run it in the terminal and it will output SQL commands for resetting the sequence on every table in the specified module.
python manage.py sqlsequencereset <your_app_label>
You can manually run any generated SQL commands, or you can pipe them into django's dbshell to run all of them automatically.
python manage.py sqlsequencereset <your_app_label> | python manage.py dbshell
See this question for more discussion on this topic.

InvalidTextRepresentation: Invalid input syntax for type bigint:"All Forms"

I had a field in my model with
book_classes = (("","Select Form"),("1",'F1'),("2",'F2'),("3",'F3'),("4",'F4'),("All Forms","All Forms"))
b_classes = models.CharField('Form',max_length=9,choices=book_classes,default="n/a")
And then changed it to
b_class =models.ForeignKey(ClassBooks,on_delete=models.CASCADE)
Where
class ClassBooks(models.Model):
name = models.CharField(max_length=10)
I'm now stuck because when I try to migrate I get an error.
Invalid input syntax for type bigint:"All Forms"
Makemigrations and migrate worked well in development. When I pushed to digital ocean, the migrate returned the error stated.
What do I need to do, please?
See Foreign Key field. By default a FK field is going to use the Primary Key of the referenced table(model), in this case the id field of ClassBooks. The id field is an integer so you get the error when trying to use a string field. To make this work, from the documentation link :
ForeignKey.to_field
The field on the related object that the relation is to. By default, Django uses the primary key of the related object. If you reference a different field, that field must have unique=True.
Which in your case becomes:
b_class =models.ForeignKey(ClassBooks,to_field='name',on_delete=models.CASCADE)
This assumes that the name field has a Unique constraint on it.
Though I am not sure how "", "1", "2" ... map to ClassBooks.name.
If you dont want to lose db.sqlite3 try to delete migrations first
Step 1: Delete the db.sqlite3 file.
Step 2 : $ python manage.py migrate
Step 3 : $ python manage.py makemigrations
Step 4: Create the super user using $ python manage.py createsuperuser
new db.sqlite3 will generates automatically

Django Model Some problems

I'm Shimul,
I've been working with Django for about 1 year - but I'm having a project problem with a problem -
When I create a model - e.g.
class postmodel(models.Model):
title = models.CharField(max_length=255)
blog_slug = models.SlugField(max_length=255,unique=True)
Then I migrated the model.
after migrating my Model [blog_slug] field I want to delete.
When I migrate the model again - and then an error occurs in the database.
The error is that the field named [blog_slug] was not found,
I don't want to delete my database - I want to [blog _slug] remove it.
What can be done to avoid this problem -
If you are in development mode and you want to delete blob_slug, remove that field from models and remove database (sqlite probably), pycache files, migations documents ( do not delete migration folder and init.py files)
Then exit virtualenv and run server again.

Django - no such column: blog_comment.body error

I am following a tutorial as I am fairly new to Django, and I am trying to add a comments system to my blog. However, whenever I try and use it I get an error message saying the following:no such column: blog_comment.body. I am not sure what is going on, as in my model I have body = models.TextField(), and I am just generally very confused.
This is because you have not run migrations in order to apply the body column to the database.
Just run ./manage.py makemigrations and ./manage.py migrate
Django will ask you to enter a default value since you have declared the body field as not nullable.
If you don't want to enter a default value, write it like this:
body = models.TextField(blank=True, null=True)
and then run the same commads.

django added a new class in a model

This thing never happened to me before,as i never had to create another class in the model field after doing syncdb already. I am now reviewing one of my past projects and i need to add another class in the models.py file. I have very little understanding of south, its more like a procedural one.
when i do this
./manage.py sql app_name
it showls the new table but when i run the server it throws an operational error 'no such table found'. Am i missing something this whole time?? Is there a way??
according to this
./manage.py sql app_name
just print sql statement for create table.
you can write it in a file
./manage.py sql app_name > command.sql
and feed it to database. for example if use postgresql you can use:
psql -U user db_name < command.sql