apache / apache/grails-core

domain with the structure of header-item failed to save in Grails 3.3.8

Open
#11,077 6 comments 0 reactions 0 assignees View on GitHub
status: pr submitted
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

### Environment Information

- **Operating System**: Ubuntu Desktop 18.04.1
- **Grails Version:** 3.2.8, 3.3.6, 3.3.8 (I only tested these three versions)
- **JDK Version:** Oracle JDK 64-bit 1.8.0_181
- **Container Version (If Applicable):** N/A
- **IDE:** IntelliJ-IDEA 2018.2.1

Domains

```
class Header {
String headerNo
Date dateCreated
Date lastUpdated
static hasMany = [items: Item]
static constraints = {
items nullable: true
}
}

class Item {
int itemNo
Date dateCreated
Date lastUpdated
static belongsTo = [header: Header]
static constraints = {
itemNo unique: 'header'
}
}

```

application.groovy is below:

```
grails.gorm.default.mapping = {
version false
createdBy length: 50
updatedBy length: 50
dateCreated sqlType:'timestamp with time zone'
lastUpdated sqlType:'timestamp with time zone'
autoTimestamp true
}
```
My database config in application.yml:

```
environments:
development:
dataSource:
dbCreate: create-drop
# url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
dialect: org.hibernate.dialect.PostgreSQLDialect
driverClassName: org.postgresql.Driver
url: jdbc:postgresql://localhost/testdb
username: user
password: user

```

build.gradle:

```
dependencies {
.... // original configures

compile "org.postgresql:postgresql:9.4.1212.jre7"
}

```
My TestController:

```
class TestController {
def createHeaderCase1() {
Header header
Header.withTransaction {
header = new Header(headerNo: 'HEADER_1')
header.addToItems(new Item(itemNo: 1))
header.addToItems(new Item(itemNo: 2))
header.save()

}
println "header dateCreated:${header.dateCreated}"
render "createHeaderCase1 test done"
}

def createHeaderCase2() {
Header header
Header.withTransaction {
header = new Header(headerNo: 'HEADER_1')
header.save()
header.addToItems(new Item(itemNo: 1))
header.addToItems(new Item(itemNo: 2))
header.save()

}
println "header dateCreated:${header.dateCreated}"
render "createHeaderCase2 test done"
}

def createHeaderCase3() {
Header.withTransaction {
Header header1 = new Header(headerNo: 'HEADER_1')
header1.addToItems(new Item(itemNo: 1))
header1.addToItems(new Item(itemNo: 2))
header1.save()

Header header2 = new Header(headerNo: 'HEADER_2')
header2.addToItems(new Item(itemNo: 1))
header2.addToItems(new Item(itemNo: 2))
header2.save()

}
render "createHeaderCase3 test done"
}
}

```
Problems:

1. execute http://localhost:8080/test/createHeaderCase1. It will success for the first time, and will fail for 2nd time.
2. execute http://localhost:8080/test/createHeaderCase2. the result depends on what database you use:
2.1 If you use h2, it will success.
2.2 If you use PostgreSQL(v10), it will lead below exception:
ERROR: null value in column "date_created" violates not-null constraint 詳細:Failing row contains (1, null, HEADER_1, 2018-08-23 08:23:53.534+08).
3. execute http://localhost:8080/test/createHeaderCase3. It will success for the first time, and will fail for 2nd time.

However, all above tests will pass when using Grails 3.2.8 (I didn't test 3.2.9~3.2.11). I think the behaviors of Grails 3.2,8 should be correct. If I am right, could Grails team fix that bug? That's very important.

Thanks

### Example Application
example app with Grails 3.3.8
[web338.zip](https://github.com/grails/grails-core/files/2312425/web338.zip)

example app with Grails 3.2.8
[web328.zip](https://github.com/grails/grails-core/files/2312423/web328.zip)

Contributor guide

Open the contributing guide

Research direction

Start by running the attached example application and reproduce createHeaderCase1, createHeaderCase2, and createHeaderCase3 in TestController with the PostgreSQL configuration from application.yml. Compare the Grails 3.3.8 setup against 3.2.8, including the mappings in application.groovy and the PostgreSQL dependency in build.gradle. Done means the repeated saves succeed and dateCreated is populated consistently.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.