Skip to content

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

Open
@tircnf

Description

@tircnf

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
    ]
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions