This is my model code
class Poll(models.Model):
created_at = models.DateTimeField(auto_now=True)
edited_at = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=200,default="X vs Y")
description = models.CharField(max_length=200,default="A poll")
def __str__(self):
return self.title
class Item(models.Model):
poll = models.ForeignKey('Poll',on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now=True)
edited_at = models.DateTimeField(auto_now_add=True)
name = models.CharField(max_length=200)
type_of = models.CharField(max_length=200)
description = models.TextField(max_length=1200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.name
You see votes in Item model right. That's a problem. I use makemigrations migrate command. But I still get no such column error.
Edit:
This is makemigrations vs_chart output
Migrations for 'vs_chart':
vs_chart\migrations\0001_initial.py:
- Create model Item
- Create model Poll
- Add field poll to item
This is migrate command output.
Operations to perform:
Apply all migrations: vs_chart
Running migrations:
No migrations to apply.
Before you add field poll to item, you may try to provide default value for ForeignKey, absence of default value may cause this issue:
poll = models.ForeignKey('Poll',on_delete=models.CASCADE, default=0)
Related
I got following error:
assert not cls._meta.auto_field, ( AssertionError: Model shop.Product can't have more than one auto-generated field )
Here is my Product class code:
class Product(models.Model):
product_id = models.AutoField(primary_key=True)
product_name = models.CharField(max_length=50)
category = models.CharField(max_length=50, default="")
subcategory = models.CharField(max_length=50, default="")
price = models.IntegerField(default=0)
desc = models.CharField(max_length=300)
pub_date = models.DateField()
image = models.ImageField(upload_to="shop/images", default="")
def __str__(self):
return self.product_name
What Am I doing wrong?
I have the same error, and i resolve doing that:
1 - Move to trash old migrate
2 - run python manage.py makemigrations
3 - run python manage.py migrate
if your problem persist, try to delete table into a data base ( if you do that, you lost your admin user, you can create another with python manage.py createsuperuser
I have made changes to one of my models in my project and migrate, makemigrations does not work as expected. Rebuilding the database creates only 2 out of 3 tables from my models.py and i cannot figure out the problem.
There are two different apps; "blog" and "users". both are registered in the setting.py.
I completely removed the database and deleted the migrations folders.
then i tried the following stuff:
django makemigrations blog
django migrate blog
doing a global django makemigrations does not have any effect, no changes are detected.
here is the relevant models.py of "blog":
class Room(models.Model):
roomname = models.CharField(max_length=6, unique=True)
roomeditors=models.ManyToManyField(User,related_name='rooms_user_can_edit', blank=True)
displayadmin=models.ForeignKey(User,
related_name='room_user_is_displayadmin',null=True, on_delete=models.SET_NULL)
def __str__(self):
return self.roomname
class Post(models.Model):
title = models.CharField(max_length=40)
content = models.TextField(max_length=300)
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
rooms = models.ManyToManyField(Room, related_name='roomposts', through='Display')
def __str__(self):
return self.title
def get_absolute_url(self):
return "/post/{}/".format(self.pk)
class Display(models.Model):
class Meta:
auto_created = True
post = models.ForeignKey(Post, on_delete=models.CASCADE)
room = models.ForeignKey(Room, on_delete=models.CASCADE)
isdisplayed = models.BooleanField(default=0)
def __str__(self):
return str(self.isdisplayed)
every table gets created except from display. the output is:
Migrations for 'blog':
blog\migrations\0001_initial.py
- Create model Room
- Create model Post
You are giving auto_created = True in your model's Meta class, which is not recommended neither its documented. Here is the list of all possible meta options you can give inside your model.
Official documentation says:
auto_created: Boolean flag that indicates if the field was automatically created, such as the OneToOneField used by model inheritance.
Giving this in Meta refrains Django to create this model itself.
Here is the exact error I am getting:
django.db.utils.ProgrammingError: relation "blog_blogtype" does not exist
LINE 1: ..."blog_blogtype"."id", "blog_blogtype"."type" FROM "blog_blog...
I have created the tables in a database in Postgres but when I try to migrate Django returns this error. My models are below. I created the column referencing to the BlogType model "type" column. I do not know what else to put here. If needed please comment it, so I can add. Thank you in advance.
class BlogType(models.Model):
id = models.AutoField(primary_key=True)
type = models.CharField(max_length=100)
def __str__(self):
return self.type
class BlogPost(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=200)
post_body = models.TextField()
time = models.DateTimeField()
category = models.ForeignKey(BlogType, related_name="type", on_delete=models.CASCADE)
def __str__(self):
return self.title
EDIT: I have found that this line in my views.py is causing the problem. I still cannot figure out how to fix it.
blog_post_types = list(BlogType.objects.all())
So i have a user that can post comments on effects, im linking up my models and i keep getting the non-nullable error no matter what ive tried. Everyone says it needs to have null=True. It isn't working lol. What am I not seeing here?
This is the official error:
django.db.utils.IntegrityError: NOT NULL constraint failed: effect_modules_comment__new.author_id
And my models:
class Effect_module(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=255)
description = models.TextField()
html = models.TextField(default='')
css = models.TextField(default='')
js = models.TextField(default='')
up_votes = models.IntegerField()
down_votes = models.IntegerField()
effect_author = models.ManyToManyField('UserProfile')
class UserProfile(models.Model):
user = models.OneToOneField(User)
effects = models.ManyToManyField(Effect_module)
class Comment(models.Model):
comment_author = models.ForeignKey(User, null=True)
comment = models.TextField(default='No Comment Here')
effect_object = models.ForeignKey(Effect_module)
Delete all migration scripts. Add null=True means it can be NULL in the database, blank=True means that it can be left blank in forms.
Then
python manage.py makemigrations
python manage.py migrate
So using south, I wanted to add a new field is_private to one of my models.
Following the tutorial, after changing the models.py file, I should do this:
./manage.py schemamigration reconstructions --auto
which returns:
Added field is_private on reconstructions.Reconstruction
Created 0005_auto__add_field_reconstruction_is_private.py. You can now apply this migration with: ./manage.py migrate reconstructions
Which is great. Now next step is,
python manage.py migrate reconstructions
And that prints:
- Migrating forwards to 0005_auto__add_field_reconstruction_is_private.
> reconstructions:0005_auto__add_field_reconstruction_is_private
- Loading initial data for reconstructions.
No fixtures found.
it seems to be doing it's job. But when I afterwards check the field is_private, Django throws me an error:
Cannot resolve keyword 'is_private' into field.
Which tells me south did not changed the database at all. Why is so?
Extra information:
The model class:
class Reconstruction(models.Model):
id = models.CharField(max_length=36, primary_key=True,
editable=False)
uploader = models.ForeignKey(User, blank=True, null=True)
status = models.TextField(blank=True)
viewcount = models.IntegerField(default=0)
error_flag = models.IntegerField(default=0)
is_done = models.BooleanField(default=False)
create_date = models.DateTimeField(auto_now=True)
last_modified_date = models.DateTimeField(auto_now=True)
is_private = models.BooleanField(default=True)
The code causing the crash:
recordings = Recording.objects.filter(is_done=True).filter(is_private=False).order_by('-create_date')
Observation: you added is_private to Reconstruction, but you're trying to filter Recording objects based on that property. Perhaps this is the issue?