#1397 - @DbForeignKey(noConstraint = true) is ignored on @ManyToMany ... foreign constraints still added

This commit is contained in:
rob bygrave
2018-05-31 10:55:58 +12:00
parent 0aa9208dcc
commit ddc00fedb6
8 changed files with 124 additions and 25 deletions
@@ -48,6 +48,40 @@ public class TestForeignKeyModes extends BaseTestCase {
assertThat(found.getOne()).isNotNull();
}
@Test
public void noneViaManyToMany() {
DfkOne one = Ebean.getReference(DfkOne.class, 999L);
DfkNoneViaMtoM none = new DfkNoneViaMtoM("none2");
none.getOnes().add(one);
// Would normally fail as DfkOne id:999 is not actually in Database
// and with the foreign key constraint on the intersection table the
// insert into the intersection table would fail
Ebean.save(none);
DfkNoneViaMtoM found = Ebean.find(DfkNoneViaMtoM.class)
.setId(none.getId())
.fetch("ones", "id")
.findOne();
assertThat(found).isNotNull();
// but we can't actually fetch it via ORM M2M ... as it joins across
// to DfkOne and that doesn't exist
assertThat(found.getOnes()).isNotNull();
assertThat(found.getOnes()).hasSize(0);
LoggedSqlCollector.start();
Ebean.delete(none);
List<String> sql = LoggedSqlCollector.stop();
assertThat(sql).hasSize(2);
assertThat(sql.get(0)).contains("delete from dfk_none_via_mto_m_dfk_one where dfk_none_via_mto_m_id = ?");
assertThat(sql.get(1)).contains("delete from dfk_none_via_mto_m where id=?");
}
@Test
public void setNullOnDelete() {
@@ -63,7 +97,6 @@ public class TestForeignKeyModes extends BaseTestCase {
DfkSetNull found = Ebean.find(DfkSetNull.class, other.getId());
assertThat(found).isNotNull();
assertThat(found.getOne()).isNull();
}
@Test