【问题标题】:How to add new Column with 'ddl-auto=update' and 'nullable=false'如何使用 'ddl-auto=update' 和 'nullable=false' 添加新列
【发布时间】:2015-11-14 19:41:46
【问题描述】:

如果我将这样的列添加到现有实体,hibernate 无法添加该列,因为存在没有值的现有行:

@Column(name = "width", nullable = false)
private int width;

失败并出现此错误:

2015-11-14 20:33:41.163 ERROR 9068 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000388: Unsuccessful: alter table texture add column width int4 not null
2015-11-14 20:33:41.163 ERROR 9068 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaUpdate  : FEHLER: Spalte „width“ enthält NULL-Werte

我不想在我的数据库列定义中使用默认值。

【问题讨论】:

    标签: java hibernate postgresql


    【解决方案1】:

    您必须使用现有数据更改表,因此如果列不可为空且未指定默认值,则无法将列添加到现有行。

    所以我认为您需要允许可为空或指定默认值,您别无选择...

    【讨论】:

    • 所以我可能会在应用程序中使用@NotNull 进行坚果空检查,并在后期版本中迁移数据库。
    猜你喜欢
    • 2020-01-21
    • 1970-01-01
    • 2023-04-05
    • 2012-01-06
    • 1970-01-01
    • 2018-06-15
    • 2021-11-27
    • 1970-01-01
    • 2020-10-31
    相关资源
    最近更新 更多