MCPcopy Create free account
hub / github.com/go-pg/pg / ExampleDB_Model_softDelete

Function ExampleDB_Model_softDelete

example_model_test.go:1109–1153  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1107}
1108
1109func ExampleDB_Model_softDelete() {
1110 type Flight struct {
1111 Id int
1112 Name string
1113 DeletedAt time.Time `pg:",soft_delete"`
1114 }
1115
1116 err := pgdb.Model((*Flight)(nil)).CreateTable(&orm.CreateTableOptions{
1117 Temp: true,
1118 })
1119 panicIf(err)
1120
1121 flight1 := &Flight{
1122 Id: 1,
1123 }
1124 _, err = pgdb.Model(flight1).Insert()
1125 panicIf(err)
1126
1127 // Soft delete.
1128 _, err = pgdb.Model(flight1).WherePK().Delete()
1129 panicIf(err)
1130
1131 // Count visible flights.
1132 count, err := pgdb.Model((*Flight)(nil)).Count()
1133 panicIf(err)
1134 fmt.Println("count", count)
1135
1136 // Count soft deleted flights.
1137 deletedCount, err := pgdb.Model((*Flight)(nil)).Deleted().Count()
1138 panicIf(err)
1139 fmt.Println("deleted count", deletedCount)
1140
1141 // Actually delete the flight.
1142 _, err = pgdb.Model(flight1).WherePK().ForceDelete()
1143 panicIf(err)
1144
1145 // Count soft deleted flights.
1146 deletedCount, err = pgdb.Model((*Flight)(nil)).Deleted().Count()
1147 panicIf(err)
1148 fmt.Println("deleted count", deletedCount)
1149
1150 // Output: count 0
1151 // deleted count 1
1152 // deleted count 0
1153}

Callers

nothing calls this directly

Calls 9

CreateTableMethod · 0.80
InsertMethod · 0.80
DeleteMethod · 0.80
WherePKMethod · 0.80
CountMethod · 0.80
DeletedMethod · 0.80
ForceDeleteMethod · 0.80
panicIfFunction · 0.70
ModelMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…