I have a Django model:
class Project(models.Model):
...
user = models.ForeignKey(User, null=True, blank=True)
product = models.ForeignKey(Product, null=True, blank=True)
I would like to change the product field to:
...
product = models.ForeignKey(Product)
But when I change it and run the South migration I get:
django.db.utils.DatabaseError: (1005, "Can't create table 'mydb.#sql-3f5_208' (errno: 121)")
Any help much appreciated.
That error appear because you use InnoDB tables, and you get 121 error because database you have some troubles with your keys.
When I got that error I resolve it by recreating database :)
You also can try to read InnoDB error log files.
P.S. Try to add some data to tables, sometimes it works.
I encountered a similar error while installing easy_thumbnails. It turned out it was a bug in South 0.7.3 which was easily fixed by installing South 0.8.1. I don't know if that's the same problem but given that you're using South it seems like it might be.
Related
I'm having some trouble with migrating from SQLite3 to Postgres on Django. Essentially I'm following this thread (Django: What are the best practices to migrate a project from sqlite to PostgreSQL). There's been a similar error that I've used the TRUNCATE command on and got past, but now I'm hitting a duplicate key error for my own model.
The model client is a OnetoOne relationship with Django's built in user. It just asks for a few additional details like business type and join date. And the migration is coming up with an error. The client, primary key 1 is my superuser with code:
user = models.OneToOneField(User, on_delete=models.SET_NULL, null=True, blank=True)
And I'm getting this error:
django.db.utils.IntegrityError: Problem installing fixture 'C:\Users\*******\dump.json': Could not load clauses.Client(pk=1): duplicate key value violates unique constraint "clauses_client_clientusername_id_******_uniq"
Could someone point me the right way? Thanks!
I have the following model at Django:
class Community(models.Model):
name = models.CharField(max_length=255)
members = models.ManyToManyField(User, through='Membership')
date_created = models.DateTimeField(auto_now_add=True)
But when I check the structure of the table (using Postico for PostgreSQL) the field of date_created after applying the migrations shows no default.
I have also tried with explicitly default=date.today() but it does not work.
Any ideas what I am missing?
Thanks,
Pablo
EDIT
Great thanks to this post: How to make a script to insert data in my default sqlite3 database django
I was trying to populate the database via script using PostgreSQL driver, when it is way simpler importing the Django models a use the create method (also thanks to Daniel Roseman in the comments that led me find the post).
I have a model like this :
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
address = models.CharField(max_length=100)
city= models.CharField(max_length=100)
, after a while, I add 2 more fields to this:
zip_code = models.CharField(max_length=20, blank=True, null=True)
state = models.CharField(max_length=50, blank=True, null=True)
, then I do the routine
python manage.py makemigrations
python manage.py migrate
But when I go to website/admin and check that model in Django Administration, I got the error "column user_profile.zip_code does not exist"
I search for the solution and some threads suggested to use South but then I learned that from django >= 1.7 we don't need to use South for migrations.
Please show me where I am wrong.
Thank you!
Check that you use the same settings when running migrate and the server.
If you are using django debug toolbar in your installed apps. Make sure to comment that, that gives the issue. If not, you can also check if your models are used by forms in another app or not. If they are its better to move the logic into views.
I commented out the debug toolbar, but that did not solve the problem.
I was adding a field to the user model. I have a utility that returns the default user. When I tried to migrate (adding the new field), that utility was getting called and was causing the error.
The solution that worked for me: in the utility that returns the default user, temporarily commenting out calls to the user table, and returning None for the default user. Migrate then ran successfully. Then of course I restored the code in the utility.
I found out that my utility was causing the problem by finding a line from my code in long list of exceptions displayed in the terminal.
I have this model (truncated here for brevity):
class Meal(models.Model):
host = models.ForeignKey(User, related_name="cooking")
cost = models.IntegerField(default=1)
summary = models.CharField(max_length=1024, default="A good dinner")
diners = models.ManyToManyField(User, through='Attendance',
related_name="diners", blank=True)
When I sync it, the diners Field is completely ignored. It doesn't appear in the database and there is no error when running syncdb. It's as if it's not there. The User and Attendance tables are all fine.
I discovered this problem when trying to add this field with South, so I've tried that as an alternative too.
Any ideas?
Thanks
Did you already run syncdb fyrir Meal before you added the diners field?
Because syncdb will not alter existing tables as you can read here:
Django docs
Side note - I have not used south personally but I have used Django evolution while developing.
Edit:
After reading your comment I think I know what the problem is.
When using through with ManyToManyField Django doesn't add a field to that table, all the necessary information is in the attendance table.
I have this model
class Vacancy(models.Model):
user = models.ForeignKey(User, null=True, blank=True, default = None)
name = models.CharField(max_length=64)
When in admin i try to creat a vacancy without a user. And it throws an error " club_vacancy.user_id may not be NULL".
Am i doing something wrong?
club_vacancy.user_id may not be NULL
Looks very much like an error from your database, rather than from Django.
It seems most likely that you added null=True after running manage.py syncdb. You'll need to modify your database schema to allow null values in that column.
Aside from South, another option is to use django evolution for schema changes.
http://code.google.com/p/django-evolution/
Install it before making db changes. Then run
python manage.py evolve --hint --execute
Make sure that if you add a new field, you allow nulls (null=True) or else evolve will give you an error message.
you need to reset you database table (since just syncdb does not update fields that are alredy created with null=False)
./manage.py reset your_app
OR if there is some data that you do not want to loose use SQL commands to remove NOT NULL flag