doc/annotation: add cascade deletion doc and example (#1340)

This commit is contained in:
Ariel Mashraki
2021-03-16 15:55:36 +02:00
committed by GitHub
parent 9685066b83
commit fc1a2a025c

View File

@@ -42,4 +42,46 @@ func (User) Fields() []ent.Field {
field.String("name"),
}
}
```
```
## Foreign Keys Configuration
Ent allows to customize the foreign key creation and provide a [referential action](https://dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html#foreign-key-referential-actions)
for the `ON DELETE` clause:
```go
package schema
import (
"entgo.io/ent"
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
)
// User holds the schema definition for the User entity.
type User struct {
ent.Schema
}
// Fields of the User.
func (User) Fields() []ent.Field {
return []ent.Field{
field.String("name").
Default("Unknown"),
}
}
// Edges of the User.
func (User) Edges() []ent.Edge {
return []ent.Edge{
edge.To("posts", Post.Type).
Annotations(entsql.Annotation{
OnDelete: entsql.Cascade,
}),
}
}
```
The example above configures the foreign key to cascade the deletion of rows in the parent table to the matching
rows in the child table.