#30966: Migration crashes due to foreign key issue, depending on otherwise
irrelevant order on MySQL.
-------------------------------------+-------------------------------------
     Reporter:  Peter Thomassen      |                    Owner:  Hasan
                                     |  Ramezani
         Type:  Bug                  |                   Status:  assigned
    Component:  Migrations           |                  Version:  master
     Severity:  Normal               |               Resolution:
     Keywords:  migration mySQL      |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Hasan Ramezani):

 * status:  new => assigned
 * owner:  nobody => Hasan Ramezani


Comment:

 I investigate it and think the problem comes from  this line:

 {{{
 
related_objects_graph[f.remote_field.model._meta.concrete_model._meta].append(f)
 }}}
 of
 
[https://github.com/django/django/blob/30359496a3f3d9af0b02afc334710f7e24c74f5b/django/db/models/options.py#L685
 django.db.models.options.Options._populate_directed_relation_graph()]:

 I added some log to check `related_objects_graph` in bothe case.
 When the migration runs by `./manage.py migrate desecapi 0001` and then
 `./manage.py migrate desecapi 0002` (correct case), here is the result:


 {{{
 defaultdict(<class 'list'>, {<Options for User>:
 [<django.db.models.fields.related.ForeignKey: owner>,
 <django.db.models.fields.related.ForeignKey: user>], <Options for Domain>:
 [<django.db.models.fields.related.ForeignKey: domain>]})
 }}}


 But, When the migration runs by `./manage.py migrate desecapi 0002` (case
 with problem),  here is the result:


 {{{
 defaultdict(<class 'list'>, {<Options for User>:
 [<django.db.models.fields.related.ForeignKey: user>], <Options for User>:
 [<django.db.models.fields.related.ForeignKey: owner>], <Options for
 Domain>: [<django.db.models.fields.related.ForeignKey: domain>]})
 }}}

 From the result, it is obvious that in the first case we have the
 `<Options for User>` key with a value of type list with two items
 `[<django.db.models.fields.related.ForeignKey: owner>,
 <django.db.models.fields.related.ForeignKey: user>]`.
 which generate two alter commands:


 {{{
 ALTER TABLE `app1_domain` DROP FOREIGN KEY
 `app1_domain_owner_id_cc7262a2_fk_app1_user_id`
 ALTER TABLE `app1_token` DROP FOREIGN KEY
 `app1_token_user_id_5ea51092_fk_app1_user_id`
 }}}

 But in the second case, we have two keys `<Options for User>`  which one
 of them has a value of type list with one item
 `[<django.db.models.fields.related.ForeignKey: user>]`
 and the other has a value of type list with one item
 `[<django.db.models.fields.related.ForeignKey: owner>]`.

 which generate just one alter command:

 {{{
 ALTER TABLE `app1_domain` DROP FOREIGN KEY
 `app1_domain_owner_id_cc7262a2_fk_app1_user_id`
 }}}

 Thus, the second foreign key `app1_token_user_id_5ea51092_fk_app1_user_id`
 still remains and cause the error.

 Any suggestions for a fix?

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30966#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/072.69ff83d9846e7ce1d25fb5569bd3bf1b%40djangoproject.com.

Reply via email to