Note that there are some explanatory texts on larger screens.

plurals
  1. POGrails Fixtures + Build-Test-Data tries to create duplicate IDs for objects
    primarykey
    data
    text
    <p>I'm trying to incorporate fixtures and build-test-data into our grails testing, yet even with the simplest example, I get an id collision when trying to build two of the same class with the build-test-data plugin. I can't help but feel like I'm missing something very simple. </p> <p>This works just fine. Assume everything that needs to be imported is imported.</p> <pre><code>class TemporaryFixturesTests extends GroovyTestCase { /* injected */ def fixtureLoader void testFixtureLoadsProperly() { def fixture = fixtureLoader.load { build { testCompany(Company, name: "TestCompany") testBasicUser(User, username: "TestUserBasic", company: testCompany) } } assert fixture.testBasicUser.company.name == "TestCompany" } void setUp() { //TODO } } </code></pre> <p>Yet this one...</p> <pre><code>class TemporaryFixturesTests extends GroovyTestCase { /* injected */ def fixtureLoader void testFixtureLoadsProperly() { def fixture = fixtureLoader.load { build { testCompany(Company, name: "TestCompany") testCompany2(Company, name: "TestCompany2") testBasicUser(User, username: "TestUserBasic", company: testCompany) testBasicUser2(User, username: "TestUserBasic2", company: testCompany2) } } assert fixture.testBasicUser.company.name == "TestCompany" assert fixture.testBasicUser2.company.name == "TestCompany2" } void setUp() { //TODO } } </code></pre> <p>breaks with the error... </p> <pre><code>nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [&lt;path removed&gt;.Company#0] </code></pre> <p>This leads me to believe that build-test-data is trying to associate id0 to both companies. That stinks. Can anyone tell me why?</p> <p>--Edit--</p> <p>It seems the tables in question are not set up to have auto-incrementing ids. So when build test data tries to make me an id column, it makes 0s every time as that is an automatically generated integer. </p> <p>osborp provided the solution for this. Build a TestDataConfig with :</p> <p>testDataConfig {</p> <pre><code>sampleData { '&lt;path removed&gt;.Company' { def i = 0 id = {-&gt; i++ } } '&lt;path removed&gt;.User' { def i = 0 id = {-&gt; i++ } } } </code></pre> <p>} </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload