Django迁移数据库列顺序 [英] Django Migration Database Column Order

查看:329
本文介绍了Django迁移数据库列顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Django 1.11,PostgreSQL 9.6和Django迁移工具.我找不到指定列顺序的方法.在初始迁移中,可以更改字段的顺序,但是migrations.AddField()调用又如何呢?对于初始迁移的外键添加,也会发生AddField调用.有什么方法可以指定顺序,或者我只是迷恋于订单,但我不应该这样做吗?

I use Django 1.11, PostgreSQL 9.6 and Django migration tool. I couldn't have found a way to specify the column orders. In the initial migration, changing the ordering of the fields is fine but what about migrations.AddField() calls? AddField calls can also happen for the foreign key additions for the initial migration. Is there any way to specify the ordering or am I just obsessed with the order but I shouldn't be?

讨论后更新

PostgreSQL DBMS不支持位置列添加.因此,期望从迁移工具中获得此功能进行列添加几乎没有意义.

PostgreSQL DBMS doesn't support positional column addition. So it is practically meaningless to expect this facility from the migration tool for column addition.

推荐答案

AFAIK,尚无官方支持的方法,因为字段应该是原子性的,并且不相关.但是,它也困扰着我的强迫症,例如,当我需要调试dbshell中的内容时,我希望对列进行排序.这是我发现您可以执行的操作:

AFAIK, there's no officially supported way to do this, because fields are supposed to be atomic and it shouldn't be relevant. However, it messes with my obsessive-compulsive side as well, and I like my columns to be ordered for when I need to debug things in dbshell, for example. Here's what I've found you can do:

  • 使用python manage.py makemigrations
  • 进行迁移
  • 编辑迁移文件并在migrations.createModel
  • 中对fields重新排序
  • Make a migration with python manage.py makemigrations
  • Edit the migration file and reorder the fields in migrations.createModel

祝你好运!

这篇关于Django迁移数据库列顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆