Unexpected GORM behavior (missing relationship table)

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Unexpected GORM behavior (missing relationship table)

maik
Hi,

there is a strange behavior in GORM:

How to reproduce:
For the domain class
> class Sample { static hasMany = [samples: Sample] }
I get two database tables (sample and sample_sample).
If i add another relationship 'Sample sample', the relationship table is
missing.

Unfortunately integration tests don't fail.

To get a test to fail, I initialize the db at BootStrap:
def init = { servletContext ->
        if (Sample.list().size() == 0) {
            new Sample().save(flush:true)
            new Sample().save(flush:true)
            new Sample().save(flush:true)
           
            Sample sampleOne = Sample.get(1)
            Sample sampleTwo = Sample.get(2)
            Sample sampleSample = Sample.get(3)
           
            sampleOne.addToSamples(sampleSample)
            assert sampleOne.samples.size() == 1
           
            sampleTwo.addToSamples(sampleSample)
            assert sampleOne.samples.size() == 1
        }
    }
and write the test db in a file:
> url = "jdbc:hsqldb:file:testDb;shutdown=true"

The first time the integration test don't fail:
> Sample sampleOne = Sample.get(1)
> assert sampleOne.samples.size() == 1
But when you run the same test again (with the already initialized db),
it fails.
Of course you can just look at the missing table in the db as well.

Can someone confirm, that this isn't how it should work?

This behavior is the same in Grails 1.3.7 and 2.0.0.RC3.

Regards,
 Maik

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Unexpected GORM behavior (missing relationship table)

Ian Roberts
On 13/12/2011 15:54, maik wrote:

> Hi,
>
> there is a strange behavior in GORM:
>
> How to reproduce:
> For the domain class
>> class Sample { static hasMany = [samples: Sample] }
> I get two database tables (sample and sample_sample).
> If i add another relationship 'Sample sample', the relationship table is
> missing.

In a normal bidirectional one to many like

class A {
   static hasMany = [bs:B]
}

class B {
   A a
}

you wouldn't get a join table as the hasMany is encoded in the
backreferences of the B instances.  Presumably GORM is applying the same
interpretation to your case (where A == B).

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Unexpected GORM behavior (missing relationship table)

maik
> In a normal bidirectional one to many like
>
> class A {
>   static hasMany = [bs:B]
> }
>
> class B {
>   A a
> }
>
> you wouldn't get a join table as the hasMany is encoded in the
> backreferences of the B instances.  Presumably GORM is applying the same
> interpretation to your case (where A == B).

Do you think, that this behavior is okay?
I don't know about the relationship table, but the test behavior looks
pretty buggy to me.

I tried it with two seperate domains (A and B instead of Sample).

The integration test
> assert aOne.bs.size() == 1
again fails after reading from database file, but not the first time
when i initialize the database.

Maik

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Unexpected GORM behavior (missing relationship table)

Jeff Scott Brown
On Mon, Dec 19, 2011 at 7:22 AM, maik <[hidden email]> wrote:

>> In a normal bidirectional one to many like
>>
>> class A {
>>   static hasMany = [bs:B]
>> }
>>
>> class B {
>>   A a
>> }
>>
>> you wouldn't get a join table as the hasMany is encoded in the
>> backreferences of the B instances.  Presumably GORM is applying the same
>> interpretation to your case (where A == B).
>
> Do you think, that this behavior is okay?
> I don't know about the relationship table, but the test behavior looks
> pretty buggy to me.
>
> I tried it with two seperate domains (A and B instead of Sample).
>
> The integration test
>> assert aOne.bs.size() == 1
> again fails after reading from database file, but not the first time
> when i initialize the database.
>
> Maik
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

The assertions in your BootStrap look to me like they should pass.
When I paste that code into a 2.0.0 test app here, they do pass.  I
think the test would fail if you got rid of the if
(Sample.list().size() == 0) test.  Is it possible you didn't have that
in place or maybe had a typo?



jb

btw… This isn't really related to your problem but instead of
Sample.list().size() == 0, you could do Sample.count() which doesn't
have to create all the instances to get you the information you need.
You don't really need the instances, you just want to know how many
there are.


--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Unexpected GORM behavior (missing relationship table)

maik
Am 19.12.2011 16:25, schrieb Jeff Brown:

>> Do you think, that this behavior is okay?
>> I don't know about the relationship table, but the test behavior looks
>> pretty buggy to me.
>>
>> I tried it with two seperate domains (A and B instead of Sample).
>>
>> The integration test
>>> assert aOne.bs.size() == 1
>> again fails after reading from database file, but not the first time
>> when i initialize the database.
>>
>> Maik
>
> The assertions in your BootStrap look to me like they should pass.
> When I paste that code into a 2.0.0 test app here, they do pass.  I
> think the test would fail if you got rid of the if
> (Sample.list().size() == 0) test.  Is it possible you didn't have that
> in place or maybe had a typo?

As I said: the test don't fail the first time.
Try to write the database in a file
> url = "jdbc:h2:testDb;MVCC=TRUE"

and run the integration test twice
> Sample sampleOne = Sample.get(1)
> assert sampleOne.samples.size() == 1

> btw… This isn't really related to your problem but instead of
> Sample.list().size() == 0, you could do Sample.count() which doesn't
> have to create all the instances to get you the information you need.
> You don't really need the instances, you just want to know how many
> there are.

Thanks

Maik


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Unexpected GORM behavior (missing relationship table)

Jeff Scott Brown
On Mon, Dec 19, 2011 at 10:19 AM, maik <[hidden email]> wrote:

> Am 19.12.2011 16:25, schrieb Jeff Brown:
>>> Do you think, that this behavior is okay?
>>> I don't know about the relationship table, but the test behavior looks
>>> pretty buggy to me.
>>>
>>> I tried it with two seperate domains (A and B instead of Sample).
>>>
>>> The integration test
>>>> assert aOne.bs.size() == 1
>>> again fails after reading from database file, but not the first time
>>> when i initialize the database.
>>>
>>> Maik
>>
>> The assertions in your BootStrap look to me like they should pass.
>> When I paste that code into a 2.0.0 test app here, they do pass.  I
>> think the test would fail if you got rid of the if
>> (Sample.list().size() == 0) test.  Is it possible you didn't have that
>> in place or maybe had a typo?
>
> As I said: the test don't fail the first time.
> Try to write the database in a file
>> url = "jdbc:h2:testDb;MVCC=TRUE"
>
> and run the integration test twice
>> Sample sampleOne = Sample.get(1)
>> assert sampleOne.samples.size() == 1
>

If you can create a sample app that demonstrates the problem and
attach it to a JIRA which describes the steps to reproduce the
problem, we will take a looksy.

Thanks for the help.



jb


--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Unexpected GORM behavior (missing relationship table)

maik
Am 19.12.2011 18:01, schrieb Jeff Brown:
> If you can create a sample app that demonstrates the problem and
> attach it to a JIRA which describes the steps to reproduce the
> problem, we will take a looksy.

Done: http://jira.grails.org/browse/GRAILS-8522

Thanks.

Maik

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email