PostgreSQL 中字符串的默认值

问题描述 投票:0回答:1

我正在尝试制作一个表格,其中几乎没有属性。我的模型是帐户具有描述属性,默认情况下应该是“此帐户没有可用的描述”

CREATE TABLE accounts
(
...
description VARCHAR DEFAULT 'No description available for this account',
...
);

在控制器描述中,仅当它不为空且不等于“时才设置 “这样

if(!description.isEmpty() && !description.equals("\n"))
{
  account.setDescription(description);
}

应用程序运行后,我得到一个账户表,但属性描述仍然为空,没有默认值。有什么想法吗?

java postgresql default
1个回答
0
投票

Postgres 仅在未提供值时设置默认值 + 列不允许使用 NULL 值。

  1. 数据库级别的处理:

在表创建脚本中

description VARCHAR DEFAULT 'No description available for this account' NOT NULL

这也会更新所有现有记录

  1. 代码中的处理 使用默认值初始化描述属性(最好来自常量文件)。

private String description = "No description available for this account";

还要对 setter 方法进行 NULL 检查。

© www.soinside.com 2019 - 2024. All rights reserved.