MCPcopy
hub / github.com/go-gorm/gorm / TestPostgresOnConstraint

Function TestPostgresOnConstraint

tests/postgres_test.go:193–241  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

191}
192
193func TestPostgresOnConstraint(t *testing.T) {
194 if DB.Dialector.Name() != "postgres" {
195 t.Skip()
196 }
197
198 type Thing struct {
199 gorm.Model
200 SomeID string
201 OtherID string
202 Data string
203 }
204
205 DB.Migrator().DropTable(&Thing{})
206 DB.Migrator().CreateTable(&Thing{})
207 if err := DB.Exec("ALTER TABLE things ADD CONSTRAINT some_id_other_id_unique UNIQUE (some_id, other_id)").Error; err != nil {
208 t.Error(err)
209 }
210
211 thing := Thing{
212 SomeID: "1234",
213 OtherID: "1234",
214 Data: "something",
215 }
216
217 DB.Create(&thing)
218
219 thing2 := Thing{
220 SomeID: "1234",
221 OtherID: "1234",
222 Data: "something else",
223 }
224
225 result := DB.Clauses(clause.OnConflict{
226 OnConstraint: "some_id_other_id_unique",
227 UpdateAll: true,
228 }).Create(&thing2)
229 if result.Error != nil {
230 t.Errorf("creating second thing: %v", result.Error)
231 }
232
233 var things []Thing
234 if err := DB.Find(&things).Error; err != nil {
235 t.Errorf("Failed, got error: %v", err)
236 }
237
238 if len(things) > 1 {
239 t.Errorf("expected 1 thing got more")
240 }
241}
242
243type CompanyNew struct {
244 ID int

Callers

nothing calls this directly

Calls 9

ClausesMethod · 0.80
NameMethod · 0.65
DropTableMethod · 0.65
MigratorMethod · 0.65
CreateTableMethod · 0.65
ExecMethod · 0.65
ErrorMethod · 0.65
CreateMethod · 0.65
FindMethod · 0.65

Tested by

no test coverage detected