(t *testing.T)
| 1670 | } |
| 1671 | |
| 1672 | func TestMigrateIgnoreRelations(t *testing.T) { |
| 1673 | type RelationModel1 struct { |
| 1674 | ID uint |
| 1675 | } |
| 1676 | type RelationModel2 struct { |
| 1677 | ID uint |
| 1678 | } |
| 1679 | type RelationModel3 struct { |
| 1680 | ID uint |
| 1681 | RelationModel1ID uint |
| 1682 | RelationModel1 *RelationModel1 |
| 1683 | RelationModel2ID uint |
| 1684 | RelationModel2 *RelationModel2 `gorm:"-:migration"` |
| 1685 | } |
| 1686 | |
| 1687 | var err error |
| 1688 | _ = DB.Migrator().DropTable(&RelationModel1{}, &RelationModel2{}, &RelationModel3{}) |
| 1689 | |
| 1690 | tx := DB.Session(&gorm.Session{}) |
| 1691 | tx.IgnoreRelationshipsWhenMigrating = true |
| 1692 | |
| 1693 | err = tx.AutoMigrate(&RelationModel3{}) |
| 1694 | if err != nil { |
| 1695 | t.Errorf("AutoMigrate err:%v", err) |
| 1696 | } |
| 1697 | |
| 1698 | // RelationModel3 should be existed |
| 1699 | _, err = findColumnType(&RelationModel3{}, "id") |
| 1700 | AssertEqual(t, nil, err) |
| 1701 | |
| 1702 | // RelationModel1 should not be existed |
| 1703 | _, err = findColumnType(&RelationModel1{}, "id") |
| 1704 | if err == nil { |
| 1705 | t.Errorf("RelationModel1 should not be migrated") |
| 1706 | } |
| 1707 | |
| 1708 | // RelationModel2 should not be existed |
| 1709 | _, err = findColumnType(&RelationModel2{}, "id") |
| 1710 | if err == nil { |
| 1711 | t.Errorf("RelationModel2 should not be migrated") |
| 1712 | } |
| 1713 | |
| 1714 | tx.IgnoreRelationshipsWhenMigrating = false |
| 1715 | |
| 1716 | err = tx.AutoMigrate(&RelationModel3{}) |
| 1717 | if err != nil { |
| 1718 | t.Errorf("AutoMigrate err:%v", err) |
| 1719 | } |
| 1720 | |
| 1721 | // RelationModel3 should be existed |
| 1722 | _, err = findColumnType(&RelationModel3{}, "id") |
| 1723 | AssertEqual(t, nil, err) |
| 1724 | |
| 1725 | // RelationModel1 should be existed |
| 1726 | _, err = findColumnType(&RelationModel1{}, "id") |
| 1727 | AssertEqual(t, nil, err) |
| 1728 | |
| 1729 | // RelationModel2 should not be existed |
nothing calls this directly
no test coverage detected