简体   繁体   English

Django 1.7迁移挂起

[英]Django 1.7 Migrations hanging

I have a django migration I am trying to apply. 我有一个django迁移我试图申请。 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). 它变得很好(它很小,它只向两个不同的模型添加一个CharField 。但是当我运行实际的迁移时它会挂起(没有失败,没有成功,只是坐着)。

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. 但是,此DB连接到连续运行的作业,新查询会立即潜入。 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. 至少在PostgreSQL中,当存在活动事务时,您无法修改表(即使它只是添加新列)。 The easiest workaround for this is usually to: 最简单的解决方法通常是:

  • run the migration script (which will hang) 运行迁移脚本(将挂起)
  • restart your webserver/wsgi container 重新启动您的webserver / wsgi容器

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. 重新启动Web服务器时,将中止所有打开的事务(假设您没有后台进程也打开事务),因此只要没有事务阻塞您的表,迁移就会完成。

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: 我发现您可以在运行事务之前立即使用以下SQL清除PostgreSQL中的任何挂起事务:

-- 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). 值得注意的是,未来读者在尝试将迁移应用于不正确大小的CharField(依赖于DB实现)时,迁移可能会挂起。 I was trying to alter a CharField to be greater than size 255 and it was just hanging. 我试图改变一个CharField大于255,它只是悬挂。 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). 即使在如上所述终止连接之后,它也不会将其修复为大小大于255的CharField,因为我的实现(postgresql)不正确。

TLDR; TLDR; Ensure your CharField is 255 or less, if greater change your CharField to a TextField and it could fix your problem! 确保您的CharField为255或更低,如果更大,将CharField更改为TextField,它可以解决您的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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