Last active
January 3, 2016 12:39
-
-
Save RubenGamarrarodriguez-tomtom/8464457 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.myapp | |
class Post { | |
String content | |
static belongsTo = [user: User] | |
static constraints = { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.myapp | |
class User { | |
String username | |
static hasMany = [posts: Post] | |
static constraints = { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.myapp | |
import org.junit.Test | |
class UserIntegrationTests { | |
@Test | |
public void testDeleteFK() { | |
def alice = new User(username: 'Alice') | |
alice.save(failOnError: true) | |
def post | |
post = new Post(user: alice, content: 'ketchup & mayo, please') | |
post.save(failOnError: true) | |
assert Post.count(), 'Post should have been saved' | |
// Since this is a test we add flush before continuing to the delete part | |
alice.save(failOnError: true, flush: true) | |
def users = User.list() | |
users.each { | |
// Flush before continuing to the verification part | |
it.delete(flush: true) //DataIntegrityViolationException | |
} | |
assert !User.count(), 'All users should have been deleted' | |
assert !Post.count(), 'All post should have been cascade deleted' | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When the
it.delete(flush: true)
is executed, I get a foreign key restriction error:If I replace:
with
the test passes.