South and Django - error on migration of unique=True - django

I have tried to change field's property - from unique=False to unique=True
and I'm getting the following error:
django.db.utils.IntegrityError: could not create unique index "xxx_fieldname_key"
DETAIL: Key (fieldname)=() is duplicated.
Any idea how to solve that?

Looks like the field you're trying to make unique already has records with duplicate value on fieldname. Check in your database if it's the case. Post your model code and full error message if not.

Related

unique_together does not replace primary key

In my Django app, I want to insert a record with a composite primary key. Apparently this should be possible by making use of "unique_together". I'm quite sure this code was working in the past, but for some reason it does not seem to be working now. This code used to run on a Linux VM, and now I'm hosting it in Google App Engine. However I don't see how this can be the cause for this error.
class TermsAndConditionsDocument(models.Model):
organization = models.ForeignKey(Organization, on_delete=models.CASCADE, verbose_name=_("Organization"))
language = models.CharField(_('Language'),choices=LANGUAGE_CHOICES, max_length=5, help_text=_("The language of the content."))
content = models.TextField()
class Meta:
unique_together = ('organization', 'language')
The error:
IntegrityError at /transactions/settings/terms_and_conditions
null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, nl-BE, <p>B</p>, 10).
According to what I've read, using "unique_together" should cause Django to not need or include an ID as primary key. I checked the database, and the ID field DOES exist. I do not understand where the database constraint and the ID field are still coming from?
Apparently, as pointed out in the comments, a primary key "id" field is always added, even if you don't need it. It's supposed to get out of your way, so you don't even notice its existence. In my case, it required me to give it a value when I created a new record, which is not how things are supposed to work.
A while back I migrated this database from one Postgres database to another Postgres database. I used an SQL dump and load method for this. Some sequences seem to have been lost during that migration.
Because there are no sequences, some fields now lacked autoincrement capabilities, explaining the IntegrityError on insertion.
In order to fix this, I did the following:
1) Export the current data:
manage.py dumpdata > data.json
2) Drop your database and create a new empty one.
3) Run database migrations:
manage.py migrate
4) Load the data again, excluding some default data already recreated by Django.
manage.py loaddata --exclude auth.permission --exclude contenttypes data.json
This procedure seems to have recreated the sequences while also keeping the data.
The unique_together only creates a DB constraint (https://docs.djangoproject.com/en/2.2/ref/models/options/#unique-together).
You could create a custom primary key with the option primary_key https://docs.djangoproject.com/en/2.2/ref/models/fields/#django.db.models.Field.primary_key but you could only do that for one field.
But I suggest to just keep the auto increment id field, this works better with Django.
For the error are you saving a model? or doing a raw import?

django.db.utils.ProgrammingError: multiple default values specified for column "id"

I was trying to change default primary key(automatic id index) to Facebook id, which now returns error I can't understand or avoid even by making everything as it was previously. So basically I'v just added this line to userporfile modal
fb_id = models.BigIntegerField(primary_key=True)
which resulted in following
django.db.utils.ProgrammingError: multiple default values specified for column "id" of table "blog_userprofile"
How can I fix it? I am using Django 1.8 + PostgreSQL
EDIT: I mean if I delete this line with fb_id the error remains

django doesn't detect migrations

I am using modeltranslation(https://github.com/deschler/django-modeltranslation) and django-eav(https://github.com/mvpdev/django-eav) apps. I want to translate django-eav app model's field description. As it is documented, i created class:
class AttributeTranslationOptions(TranslationOptions):
fields = ('description', )
and registered it: translator.register(Attribute, AttributeTranslationOptions). Basically this creates additional fields for languages that my project supports, in my case it's 4. It should be: description_en, description_ru and so on.. This works fine with my other models, but when i try to translate Attribute class, it says (1054, "Unknown column 'eav_attribute.description_en' in 'field list'"). Then i try to migrate, but it doesn't detect changes. Tried south schemamigration app_name --auto, also doesn't work.
How can i solve this?
Thanks
EDITED:
Tried python manage.py schemamigration eav --initial. Now it finds these fields:
+ Added field description_ru on eav.Attribute
+ Added field description_lv on eav.Attribute
+ Added field description_en on eav.Attribute
BUT, then another error occurs: FATAL ERROR - The following SQL query failed: CREATE TABLE eav_enumvalue (id integer AUTO_INCREMENT NOT NULL PRIMARY KEY, value varchar(50) NOT NULL UNIQUE, icon varchar(300) NULL)
The error was: (1050, "Table 'eav_enumvalue' already exists")

Why is AutoField not working?

I created this field in my model:
numero_str = models.AutoField(primary_key=True, unique=True, default = 0)
The default value seems to invalidate the auto increment of AutoField but if I take it out I receive an error saying that the field can't be null. What I don't understand is: if I declareted it as a AutoField, isn't it supposed to generate a sequencial integer automatically? Or should I declare something when saving an item?
To be more especific, my app is basically a form that is send by e-mail and saved in a database. The error occur when sending (in case I take out the default value). It says:
IntegrityError at /solicitacaodetreinamento/
str_solicitacao.numero_str may not be NULL
I found a solution: I deleted the DB file and all the migration of South (include the Initial). Then I recreated the database and made the migrations. This time, using the default primary key, i.e., "id". In my case it was simpler because I had no real data at all (it is not in production), otherwise I would have to export the data, recreate the database and then import the data. Thank you all.

Django Column 'id' cannot be null

I am having a weird problem with my mysql database and django.
I created an app with a model imported from an existing database with inspectdb. It was working fine until yesterday I removed the django automatically created tables (django_, auth_, site_*). I did that because it was preventing my model to validate when I added my app in the installed apps.
Now today I tried inserting a new record in my database and I get the following error :
Django Version: 1.3
Exception Type: IntegrityError
Exception Value: (1048, "Column 'asset_id' cannot be null")
The field is a primary key and it's supposed to be auto_increment so I don't give a value to it when I create a new record.
Can someone point me what's going on here ?
EDIT : I partly figured out the problem : somehow all my auto_increment proprities were removed from my database. How did that happen ?
Check your generated models, and ensure that the "auto_increment" columns correspond with fields marked with the primary_key=True option.
May be you need to save your object model, before obtain id property.