Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema Generation creates bad joinTable when multiple associations point to the same type of object. #1676

Open
4 tasks done
tircnf opened this issue Sep 14, 2022 · 0 comments

Comments

@tircnf
Copy link

tircnf commented Sep 14, 2022

Task List

  • Steps to reproduce provided
  • Stacktrace (if present) provided
  • Example that reproduces the problem uploaded to Github
  • Full description of the issue provided (see below)

Steps to Reproduce

  1. Create two domain classes.
  2. Make two one-to-many associations between the two objects.
  3. Try and save a value to the association.

Expected Behaviour

Associations should persist to the database.

Actual Behaviour

The join table that is created has NOT NULL constraints for each association.
When an insert happens, the NOT NULL constraint for the other association fires and prevents the save.

Environment Information

  • Operating System: WINDOWS/UNIX
  • GORM Version: 7.3.2
  • Grails Version (if using Grails): 5.2.3
  • JDK Version: 1.8

Example Application

https://github.com/tircnf/gormExmple

The example application only has one test spec file, included below.
When hibernate starts, it creates a single join table for the two associations with a
person_petsilike_id column and a person_petsihate_id column, both with a not-null constraint.

when inserting an association a null constraint fires for the other association.

Here is the create table command:

    create table person_pet (
       person_petsilike_id bigint not null, 
       pet_id bigint, 
       person_petsihate_id bigint not null
    )

and here is the error when trying to add a pet that I like.

NULL not allowed for column "PERSON_PETSIHATE_ID"; SQL statement: insert into person_pet (person_petsilike_id, pet_id) values (?, ?) [23502-200]

import grails.persistence.Entity
import grails.test.hibernate.HibernateSpec

class MultiRelationSpec extends HibernateSpec {

    @Override
    List<Class> getDomainClasses() {
        [Pet, Person]
    }

    def setup() {
    }

    def cleanup() {
    }

    void "Test setup"() {
        expect: "I can create my local entities"
        new Person(name: "name").save(flush: true, failOnError: true)
        new Pet(name: "name").save(flush: true, failOnError: true)
    }

    void "Test adding a relationship"() {
        expect:
        Person p = new Person(name: "person").save()
        Pet friendly = new Pet(name: "friendly").save()
        Pet mean = new Pet(name: "mean")

        p.addToPetsILike(friendly)
        p.save(flush:true)

    }
 }


@Entity
class Pet {
    String name
}

@Entity
class Person {
    String name

    static hasMany = [
            petsILike: Pet,
            petsIHate: Pet
    ]
}
@tircnf tircnf changed the title Schema Generation problems with multiple associations against the same object. Schema Generation creates bad joinTable when multiple associations point to the same type of object. Sep 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant