postgresql - 仅对非空字符串列应用唯一索引
问题描述
我的 Postgres DB 中有一个字符串列,它有一个唯一索引,但是当该列为空时,我想在某些行中允许重复值。
我希望我的唯一索引允许空值。我怎样才能做到这一点?
我的 postgres 模型的当前代码:
my_field string `gorm:"uniqueIndex"`
解决方案
您可以创建部分索引:
create unique index on the_table (the_column)
where the_column <> '';
根据您的要求,您可能还想排除其中the_column
的行null
推荐阅读
- python - 计算长度最多为 6 的不同素数子串的总数
- javascript - 使用 Javascript,我如何定位和更改没有 ID 或类的图像的 URL?
- javascript - 在开始拖动和拖动中更改光标
- asp.net-core-mvc - 如何在 ASP.NET Core 的另一个项目中使用控制器和视图
- unity3d - Unity C#按钮onClick方法如何自动获取游戏对象
- java - Hadoop 连接拒绝到 localhost:8032 上的 ResourceManager
- mysql - 附加到 JSON 字段的 MySQL 的 GoLang 查询
- php - PHP DOMDocument 无法创建-tag
- java - Can't succeed in using multiple textures in a fragment
- android - [Unity][Firebase] Dex:将字节码转换为 dex 时出错:构建播放器时出错