﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
25955	FK constraints are not checked at the end of nested atomic blocks	Artyem Klimenko	nobody	"{{{
#!div style=""font-size: 80%""
Create 2 models:
  {{{#!python
class Model1(models.Model):
    pass

class Model2(models.Model):
    model1 = models.ForeignKey(Model1)
  }}}
}}}

{{{
#!div style=""font-size: 80%""
foreign key in DB:
  {{{#!postgresql
  CONSTRAINT zabbix_model2_model1_id_4d6fe5808c3891b4_fk_zabbix_model1_id FOREIGN KEY (model1_id)
      REFERENCES zabbix_model1 (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED
-- INITIALLY DEFERRED !!!!!!!!!!!!!!!!!!!
-- DEFERRABLE constraints set to DEFERRED (INITIALLY DEFERRED or via SET CONSTRAINTS) are checked after each transaction (not savepoint release).
  }}}
}}}


{{{
#!div style=""font-size: 80%""
add row in model1:
  {{{#!python
>>> Model1().save()
>>> Model1.objects.values()
[{'id': 1}]

  }}}
}}}

{{{
#!div style=""font-size: 80%""
view:
  {{{#!python
@transaction.atomic()
def _test(request):
    model1_ids = [
        1,
        2,  # incorrect ForeignKey
        1.
    ]
    for model1_id in model1_ids:
        try:
            with transaction.atomic():
                Model2(model1_id=model1_id).save()
        except IntegrityError as e:
            # magic
            pass
    return render(request, 'test_template.html')
  }}}
}}}

> Request Method: 	GET
> Request URL: 	http://127.0.0.1:8000/test
> Django Version: 	1.8.6
> Exception Type: 	IntegrityError
> Exception Value: 	
> insert or update on table ""zabbix_model2"" violates foreign key constraint ""zabbix_model2_model1_id_4d6fe5808c3891b4_fk_zabbix_model1_id""
> DETAIL:  Key (model1_id)=(2) is not present in table ""zabbix_model1"".
> Exception Location: 	/home/its/venv/lib/python3.4/site-packages/django/db/backends/base/base.py in _commit, line 142
> Python Executable: 	/home/its/venv/bin/python
> Python Version: 	3.4.2

[[Image(http://i5.imageban.ru/out/2015/12/20/58dc28d049b669a5633a8a7b9643234b.png)]]"	Bug	new	Database layer (models, ORM)	dev	Normal		DEFERRED transaction.atomic savepoint postgresql	aklim007@… Ülgen Sarıkavak	Someday/Maybe	1	0	0	1	0	0
