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")
Related
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
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?
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.
I am trying to migrate a MySQL-based Django project to PostgreSQL. Unfortunately, all sql/syncdb commands fail as soon as I switch the database backend in settings.py.
python manage.py sql profile w/ MySQL:
BEGIN;
CREATE TABLE `profile_department` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`name` varchar(100) NOT NULL UNIQUE,
[...]
python manage.py sql profile w/ PostgreSQL (psycopg2):
DatabaseError: relation "profile_department" does not exist
LINE 1: ...nt"."homepage", "profile_department"."gd_id" FROM "profile_d...
^
Why does Django behave differently with the two DB backends?
Thomas was right, there were indeed two queries buried somewhere in models.py that referred to the Department class:
ROOT_DEPARTMENT = Department.objects.get(pk=14)
FACULTIES = Department.objects.filter(parent=ROOT_DEPARTMENT)
Thanks!
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.