apache / apache/grails-core

Removing an element from a list doesn't actually remove it

Open
#14,434 0 comments 0 reactions 0 assignees View on GitHub
relates-to: grails-data-hibernate5
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

Hello. I hope this is the right place for this issue, and that it has enough information:

So I have these two classes:
```
class Lol {

Long id
String name

List omgs = new ArrayList<>()

static hasMany = [
omgs: Omg
]

static mappedBy = [
omgs: 'lol'
]

static mapping = {
id generator: 'sequence', params:[sequence:'lol_seq']
omgs indexColumn: [
name: 'lol_index'
]
}
}
```

```
package ai.boost

class Omg {

Long id
String name

Lol lol

static constraints = {
lol nullable: true
}

static mapping = {
id generator: 'sequence', params: [sequence: 'omg_seq']
}
}
```

And the SQL:
```
CREATE SEQUENCE lol_seq;
CREATE TABLE lol (
id BIGINT PRIMARY KEY DEFAULT NEXTVAL('lol_seq'),
name text
);

CREATE SEQUENCE omg_seq;
CREATE TABLE omg (
id BIGINT PRIMARY KEY DEFAULT NEXTVAL('omg_seq'),
name text,
lol_id BIGINT REFERENCES lol(id),
lol_index INT
);
```

Basically, a Lol has an ordered list of omgs.

The following code works fine:
```
Lol lol = new Lol(name: 'Lol 1');
Omg omg1 = new Omg(name: 'Omg 1')
Omg omg2 = new Omg(name: 'Omg 2')
Omg omg3 = new Omg(name: 'Omg 3')
lol.addToOmgs(omg1)
lol.addToOmgs(omg2)
lol.addToOmgs(omg3)
lol.save(flush: true)
```
I mean, look at this, it's just what I want:
![image](https://user-images.githubusercontent.com/30621356/92895731-0f3ae100-f41c-11ea-883f-3e5f203fffa4.png)
Beatiful!

But, what happens if I want to remove one of the omgs from the list _without deleting it_?
```
Lol lol = new Lol(name: 'Lol 1');
Omg omg1 = new Omg(name: 'Omg 1')
Omg omg2 = new Omg(name: 'Omg 2')
Omg omg3 = new Omg(name: 'Omg 3')
lol.addToOmgs(omg1)
lol.addToOmgs(omg2)
lol.addToOmgs(omg3)

lol.save(flush: true)

lol.removeFromOmgs(omg2) // Just added this line
```
This:
![image](https://user-images.githubusercontent.com/30621356/92895225-976cb680-f41b-11ea-9ce8-497321d4837d.png)
What is this? `lol_id` is still `1`, but, in my opinion, should be `NULL`, and the same with `lol_index`.

Things I tried that had no effect:
- `omg2.setLol(null)`
- Adding `nullable: true` to the `indexColumn`
- Upgrading from gorm 6.1.9 to 6.1.12

Contributor guide

Open the contributing guide

Research direction

Reproduce the ordered association using the Lol and Omg classes, the removeFromOmgs call, and the shown SQL schema. Start by tracing the GORM 6.1.9–6.1.12 handling of removal from a mapped, indexed hasMany list. Done means removing omg2 without deleting it clears its lol_id and lol_index while preserving the other association rows.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.