On 1/10/2015 1:36 PM, Daniel Chen wrote:
I have tried that (by the way, the offending migration didn't show up in
the django_migrations table because the migration failed).

I think we might be talking past each other. Sorry if I'm being unclear.
Here's an example of what's going on:

You haven't mentioned the Python/Django version. Does ticket https://code.djangoproject.com/ticket/23226 apply?


 1. Before migration, my database is at state X, and my migrations files
    reflect that.
 2. I change my model file, updating the state (let's call it state Y).
    Then, I run make migrations, generating a new migration file that
    corresponds to state Y.
 3. I run the migrate command to get my database to state Y. It fails,
    so now my database is back at state X. There is nothing in the
    database to delete (not even from the django_migrations table)
    because the transaction was rolled back.
 4. I delete the migration file that corresponds to state Y. My database
    is back in state X, and my migration files again reflect that.
 5. I run the migrate command again, and it fails (same problem). The
    error is django.db.models.fields.FieldDoesNotExist: [Model] has no
    field named [field].
 6. [field] corresponds to a new field in state Y, not in an existing
    field in state X. I'm confused - why is it looking for a field that
    doesn't exist in the pre-migrated state (state X)?
 7. So, I assume that the model state somehow is de-synced from the
    database/migration state. I hop into the Django shell and check the
    model metadata. The fields exactly match state Y. My database is
    still in state X.

Does that make sense?

On Wednesday, September 30, 2015 at 4:10:18 PM UTC-7, Mike Dewhirst wrote:

    On 1/10/2015 6:51 AM, Daniel Chen wrote:
     > Thanks for the reply. I should have mentioned that I tried all of
    that:
     > the problem is the model state is ahead of the database state, not
     > behind it. It seems that the database migration was atomic, so
     > everything was rolled back as that transaction failed, but the same
     > thing didn't happen at the model level. So, now I'm stuck with a
    model
     > state that the previous migrations don't capture.

    The model is the master. It is outside the transaction and will
    never be
    touched by migrations.

    Your task is to identify which previous migration matches the current
    database structure and delete all migrations after that point. That
    means deleting the migration files after the one you identified AND
    deleting the migration records in django_migrations in the database.

    Thereafter, the next migration will pick up all the changes in the
    model
    and bring the database back into alignment.

    This is all much the same as I said last time so if this doesn't
    work it
    means I haven't understood your problem.

     >
     > On Wednesday, September 30, 2015 at 5:08:22 AM UTC-7, Mike
    Dewhirst wrote:
     >
     > Â  Â  On 30/09/2015 4:16 PM, Daniel Chen wrote:
     > Â  Â  Â > Hi all,
     > Â  Â  Â >
     > Â  Â  Â > A migration I was running (manage.py migrate) died in
    the middle. I
     > Â  Â  Â > deleted the migration file, fixed the problem (wrong
    default value),
     > Â  Â  Â > recreated the migration, and re-ran it. But now, I'm
    getting an
     > Â  Â  Â > inconsistent state (error message: [Model] has no field
    named
     > Â  Â  [field]).
     > Â  Â  Â > When I check the database, all the old fields are
    there. However,
     > Â  Â  when I
     > Â  Â  Â > hop into a Django shell (manage.py shell) and check the
    model fields
     > Â  Â  Â > ([Model]._meta.fields), they reflect the new,
    post-migration
     > Â  Â  state. What
     > Â  Â  Â > is going on, and how do I fix this?
     >
     > Â  Â  Do it again. Delete the migration. Take note of the last valid
     > Â  Â  migration. Open the django_migrations table and delete the
    migration
     > Â  Â  record(s) after the last valid migration.
     >
     > Â  Â  makemigrations again and then migrate again to bring the
    database into
     > Â  Â  line with your models.
     >
     > Â  Â  BTW, the shell is looking at the model not the database.
     >
     > Â  Â  Mike
     >
     > Â  Â  Â >
     > Â  Â  Â > Thanks!
     > Â  Â  Â >
     > Â  Â  Â > --
     > Â  Â  Â > You received this message because you are subscribed to
    the Google
     > Â  Â  Â > Groups "Django users" group.
     > Â  Â  Â > To unsubscribe from this group and stop receiving
    emails from it,
     > Â  Â  send
     > Â  Â  Â > an email to django-users...@googlegroups.com <javascript:>
     > Â  Â  Â > <mailto:django-users+unsubscr...@googlegroups.com
    <javascript:> <javascript:>>.
     > Â  Â  Â > To post to this group, send email to
    django...@googlegroups.com
     > Â  Â  <javascript:>
     > Â  Â  Â > <mailto:django...@googlegroups.com <javascript:>>.
     > Â  Â  Â > Visit this group at
    http://groups.google.com/group/django-users
    <http://groups.google.com/group/django-users>
     > Â  Â  <http://groups.google.com/group/django-users
    <http://groups.google.com/group/django-users>>.
     > Â  Â  Â > To view this discussion on the web visit
     > Â  Â  Â >
     > Â  Â
    
https://groups.google.com/d/msgid/django-users/95f33338-c53b-497d-8dcd-a4c1e444b5c4%40googlegroups.com
    
<https://groups.google.com/d/msgid/django-users/95f33338-c53b-497d-8dcd-a4c1e444b5c4%40googlegroups.com>

     > Â  Â
    
<https://groups.google.com/d/msgid/django-users/95f33338-c53b-497d-8dcd-a4c1e444b5c4%40googlegroups.com
    
<https://groups.google.com/d/msgid/django-users/95f33338-c53b-497d-8dcd-a4c1e444b5c4%40googlegroups.com>>

     >
     > Â  Â  Â >
     > Â  Â
    
<https://groups.google.com/d/msgid/django-users/95f33338-c53b-497d-8dcd-a4c1e444b5c4%40googlegroups.com?utm_medium=email&utm_source=footer
    
<https://groups.google.com/d/msgid/django-users/95f33338-c53b-497d-8dcd-a4c1e444b5c4%40googlegroups.com?utm_medium=email&utm_source=footer>

     > Â  Â
    
<https://groups.google.com/d/msgid/django-users/95f33338-c53b-497d-8dcd-a4c1e444b5c4%40googlegroups.com?utm_medium=email&utm_source=footer
    
<https://groups.google.com/d/msgid/django-users/95f33338-c53b-497d-8dcd-a4c1e444b5c4%40googlegroups.com?utm_medium=email&utm_source=footer>>>.

     >
     > Â  Â  Â > For more options, visit
    https://groups.google.com/d/optout <https://groups.google.com/d/optout>
     > Â  Â  <https://groups.google.com/d/optout
    <https://groups.google.com/d/optout>>.
     >
     > --
     > You received this message because you are subscribed to the Google
     > Groups "Django users" group.
     > To unsubscribe from this group and stop receiving emails from it,
    send
     > an email to django-users...@googlegroups.com <javascript:>
     > <mailto:django-users+unsubscr...@googlegroups.com <javascript:>>.
     > To post to this group, send email to django...@googlegroups.com
    <javascript:>
     > <mailto:django...@googlegroups.com <javascript:>>.
     > Visit this group at http://groups.google.com/group/django-users
    <http://groups.google.com/group/django-users>.
     > To view this discussion on the web visit
     >
    
https://groups.google.com/d/msgid/django-users/559fd3eb-36e4-4ace-a270-3caf1e7fd874%40googlegroups.com
    
<https://groups.google.com/d/msgid/django-users/559fd3eb-36e4-4ace-a270-3caf1e7fd874%40googlegroups.com>

     >
    
<https://groups.google.com/d/msgid/django-users/559fd3eb-36e4-4ace-a270-3caf1e7fd874%40googlegroups.com?utm_medium=email&utm_source=footer
    
<https://groups.google.com/d/msgid/django-users/559fd3eb-36e4-4ace-a270-3caf1e7fd874%40googlegroups.com?utm_medium=email&utm_source=footer>>.

     > For more options, visit https://groups.google.com/d/optout
    <https://groups.google.com/d/optout>.

--
You received this message because you are subscribed to the Google
Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to django-users+unsubscr...@googlegroups.com
<mailto:django-users+unsubscr...@googlegroups.com>.
To post to this group, send email to django-users@googlegroups.com
<mailto:django-users@googlegroups.com>.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/0ac300f7-0354-4716-bd9b-5552940ae26a%40googlegroups.com
<https://groups.google.com/d/msgid/django-users/0ac300f7-0354-4716-bd9b-5552940ae26a%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/560CAF14.2040802%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

Reply via email to