简体   繁体   中英

Django 1.7 Migrations hanging

I have a django migration I am trying to apply. It gets made fine (it's small, it's only adding a CharField to two different Models. However when I run the actual migrate it hangs (no failure, no success, just sits).

Through googling I've found that other open connections can mess with it so I restarted the DB. However this DB is connect to continuously running jobs and new queries do sneak in right away. However they are small, and last time I tried restarting I THINK I was able to execute my migrate before anything else. Still nothing.

Are there any other known issues that cause something like this?

At least in PostgreSQL you cannot modify tables (even if it's just adding new columns) while there are active transactions. The easiest workaround for this is usually to:

  • run the migration script (which will hang)
  • restart your webserver/wsgi container

When restarting your webserver all open transactions will be aborted (assuming you don't have background processes which also have transactions open), so as soon as no transactions are blocking your table, the migration will finish.

I was having this same problem today. I discovered that you can clear out any hanging transactions in PostgreSQL using the following SQL immediately before running your transaction:

-- View all the current activity
-- SELECT * FROM pg_stat_activity;

-- terminate other connections (make sure to add your own IP address)
SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE client_addr <> 'YOUR IP HERE'

This will terminate any connections that aren't yours, which might not be ideal in all circumstances, but works like a charm.

Worth noting for future readers that the migrations can hang when trying to apply a migration for an incorrect size CharField (DB implementation dependent). I was trying to alter a CharField to be greater than size 255 and it was just hanging. Even after terminating the connections as stated it would not fix it as a CharField of size greater than 255 as that was incorrect with my implementation (postgresql).

TLDR; Ensure your CharField is 255 or less, if greater change your CharField to a TextField and it could fix your problem!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM