ModelForm tries INSERT, but UPDATE would be correct
guettli opened this issue · 1 comments
guettli commented
If you use ModelForm of a django typed model, the form of a child class does not see the objects form the parent class.
In the following example there is already a config if type BaseConfig. If
I save the SpecialConfigForm I get the following exception:
class BaseConfig(typedmodels.TypedModel):
unique_fk=models.ForeignKey(ModelWhichShouldBeConfigured, unique=True)
class SpecialConfig(BaseConfig):
pass
class SpecialConfigForm(forms.ModelForm):
class Meta:
model=SpecialConfig
exclude=['type']
If there is already a BaseConfig, then SpecialConfigForm tries an INSERT, but an UPDATE should happen.
File "/home/foo_eins_d/src/foo-myapp/foo_myapp/tests/unit/test_forms.py", line 26, in test_myapp_special_config_form__create
form.save()
File "/home/foo_eins_d/local/lib/python2.7/site-packages/django/forms/models.py", line 370, in save
fail_message, commit, construct=False)
File "/home/foo_eins_d/local/lib/python2.7/site-packages/django/forms/models.py", line 87, in save_instance
instance.save()
File "/home/foo_eins_d/local/lib/python2.7/site-packages/typedmodels/models.py", line 365, in save
return super(TypedModel, self).save(*args, **kwargs)
File "/home/foo_eins_d/local/lib/python2.7/site-packages/django/db/models/base.py", line 546, in save
force_update=force_update, update_fields=update_fields)
File "/home/foo_eins_d/local/lib/python2.7/site-packages/django/db/models/base.py", line 591, in save_base
update_fields=update_fields)
File "/home/foo_eins_d/local/lib/python2.7/site-packages/django/db/models/base.py", line 650, in save_base
result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/home/foo_eins_d/local/lib/python2.7/site-packages/django/db/models/manager.py", line 215, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File "/home/foo_eins_d/local/lib/python2.7/site-packages/django/db/models/query.py", line 1675, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/foo_eins_d/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 943, in execute_sql
cursor.execute(sql, params)
File "/home/foo_eins_d/local/lib/python2.7/site-packages/django/db/backends/util.py", line 41, in execute
return self.cursor.execute(sql, params)
File "/home/foo_eins_d/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 56, in execute
six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
File "/home/foo_eins_d/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 54, in execute
return self.cursor.execute(query, args)
IntegrityError: duplicate key value violates unique constraint "foo_unique_fk_uniq"
DETAIL: Key (some_unique_key)=(2081, myappTest) already exists.
I have not found the details, yet. I hope to investigate this in the next days. I will post more details, if I found a solution.
I am not sure if this is related to django typed models. Maybe this would happen with multi table inheritance, too.
guettli commented
I can't reproduce it. Closing it.