【问题标题】:django add model field manualy to postgres databasedjango 手动将模型字段添加到 postgres 数据库
【发布时间】:2014-01-01 15:41:35
【问题描述】:

我不能使用 south,也没有 pgAdmin,但我需要在模型末尾添加新字段。我需要:

new_field = models.CharField(max_length=200, blank=True, null=True)

我有 ssh 访问权限并定义了 psql 用户,所以我需要语法来将该字段添加到模型数据库表中。 ALTER TABLE store_products ADD COLUMN description text; 能完成这项工作吗?如何将 max_length 200、空白和 null 设置为 true? postgresql 9.1,django 1.6。

【问题讨论】:

标签: django postgresql django-models postgresql-9.1 ddl


【解决方案1】:

我认为blank=Truenull=True 表示应该允许空字符串和NULL 值。这些是默认值,您无需执行任何额外操作。

对于max_length=200,您可以使用varchar(200) 而不是text

ALTER TABLE store_products ADD COLUMN description varchar(200);

但我通常更喜欢将数据类型 textCHECK constraint 结合使用:

ALTER TABLE store_products ADD COLUMN description text;
ALTER TABLE store_products ADD CONSTRAINT description_max200
CHECK (length(description) <= 200);

【讨论】:

    猜你喜欢
    • 2018-11-01
    • 2014-04-02
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 2018-08-03
    • 1970-01-01
    • 2014-08-05
    • 2021-08-30
    相关资源
    最近更新 更多