From 7d55bbca3640d2c9a419b8893fc9eee63778f116 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 19 Jun 2020 23:08:02 +0800 Subject: [PATCH] Add On Delete/Update Constraint --- GORM-V2-Release-Note-Draft-CN.md | 18 +++++++++++++++++- GORM-V2-Release-Note-Draft.md | 18 +++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/GORM-V2-Release-Note-Draft-CN.md b/GORM-V2-Release-Note-Draft-CN.md index 369f582..c9c24ff 100644 --- a/GORM-V2-Release-Note-Draft-CN.md +++ b/GORM-V2-Release-Note-Draft-CN.md @@ -12,7 +12,7 @@ GORM 2.0 现处于公测阶段,已部署于多个用户的生产服务环境 * 代码模块化 * Context、批量插入、Prepared Statment、DryRun 模式、Join Preload, Find 到 Map, FindInBatches 支持 * SavePoint/RollbackTo/Nested Transaction 支持 -* 关联关系优化,多对多中间表替换优化,Association 模式对批量数据支持及优化 +* 关联关系优化 (On Delete/Update),多对多中间表替换优化,Association 模式对批量数据支持及优化 * SQL Builder 优化, Upsert, Locking, Optimizer/Index/Comment Hints 支持 * 插入时间、更新时间可支持多个字段,加入了对 unix (nano) second 的支持 * 字段多重权限支持:只读、只允许创建、只允许更新、只写、完全忽略 @@ -255,6 +255,22 @@ type Blog struct { } ``` +#### 关联 On Delete/Update 支持 + +```go +type Profile struct { + gorm.Model + Refer string + Name string +} + +type User struct { + gorm.Model + Profile Profile `gorm:"Constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` + ProfileID int +} +``` + ### 多对多中间表替换优化 可以更简单的来设置 `Many2Many` 的 `JoinTable`,替换后的 `JoinTable` 可获得普通模型的全部功能,例如 `Soft Delete`,`Hooks` 等功能支持,也可以添加更多字段 diff --git a/GORM-V2-Release-Note-Draft.md b/GORM-V2-Release-Note-Draft.md index c1a4bc3..56ad55a 100644 --- a/GORM-V2-Release-Note-Draft.md +++ b/GORM-V2-Release-Note-Draft.md @@ -12,7 +12,7 @@ GORM 2.0 is not yet released, currently, in the public beta stage, it has been u * Modularity * Context, Batch Insert, Prepared Statment, DryRun Mode, Join Preload, Find To Map, FindInBatches * SavePoint/RollbackTo/Nested Transaction Support -* Association improvements, Modify Join Table for Many2Many, Association Mode for batch data +* Association improvements (On Delete/Update), Modify Join Table for Many2Many, Association Mode for batch data * SQL Builder, Upsert, Locking, Optimizer/Index/Comment Hints supports * Multiple fields support for auto creating/updating time, which also support unix (nano) seconds * Field permissions support: readonly, writeonly, createonly, updateonly, ignored @@ -256,6 +256,22 @@ type Blog struct { } ``` +#### Association On Delete/Update Support + +```go +type Profile struct { + gorm.Model + Refer string + Name string +} + +type User struct { + gorm.Model + Profile Profile `gorm:"Constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` + ProfileID int +} +``` + ### Modify Join Table for Many2Many Easier to setup Many2Many's `JoinTable`,the `JoinTable` can be a full-featured model, like having `Soft Delete`,`Hooks` supports, and define more fields