记postgresql sequelize ORM upsert 踩坑

踩到的坑:发现upsert 只插入不能更新。

最近禁网又不能翻墙,所以百度了一圈没找到答案,只能读官方文档,折腾半小时~,泪奔

解决:

在Model层也要声明唯一索引,要不然不生效。


先看下官方文档:

public static upsert(values: Object, options: Object): Promise<boolean>

Insert or update a single row. An update will be executed if a row which matches the supplied values on either the primary key or a unique key is found.

 (Note that the unique index must be defined in your sequelize model and not just in the table.)特别注意,刚开始眼瞎没看到~

 Otherwise you may experience a unique constraint violation, because sequelize fails to identify the row that should be updated.

 

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容