| 191 | } |
| 192 | |
| 193 | func 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 | |
| 243 | type CompanyNew struct { |
| 244 | ID int |