I'm getting this error when I'm trying to delete a Interviewee in the GORM :
Referential integrity constraint violation: "FK_APCC8LXK2XNUG8377FATVBN04:
PUBLIC.USER_ROLE FOREIGN KEY(USER_ID) REFERENCES PUBLIC.USERS(ID) (40)";
SQL statement: delete from users where id=? and version=? [23503-176]
And this is my domain :
package com.cgi.recruitmenttest
import com.cgi.security.User
class Interviewee extends User{
String firstName
String lastName
String email
String telephone
String level
static hasMany = [results:Result,tests:TestInterviewe]
static constraints = {
lastName()
firstName()
email(email: true)
telephone(nullable: true)
level inList: ['Debutant', 'Confirme', 'Expert']
}
}
I just try to create a interviewee without results and tests but when I delete, i get this error..
Can someone help ? Thanks
You want to delete a data from USERS table but in USER_ROLE table it contained a foreign key (named USER_ID) of USERS table. That's why you are unable to delete. Delete USER_ROLE table's data first according USERS table ID then you may able to delete.
Related
I'm making a model where a User fills out many questionnaires and the responses get saved to Questionresponse. I'm on grails 2.5.2
Test1
So I have two models
class User {
String username
...
static hasMany = [reponse: QuestionResponse]
}
class QuestionResponse {
String question_1
String question_2
...
}
With the above, a new DB table is created: user_questionresponse with two columns user_questionresponses_id and questionresponse_id. This seems like what I want. A user would have many questionresponses and those relationships would be saved in this table. However, I can't find out how to save data to this table.
For example, if I do:
def user = springSecurityService.currentUser
def questionnaire = new QuestionResponse(question_1: "foo", question_2: "bar")
//How do I link the user to this newly created questionnaire?
user.addToResponse(q).save(flush: true) //DOES NOT WORK.
Test2 (just add belongsTo)
class User {
String username
...
static hasMany = [reponse: QuestionResponse]
}
class QuestionResponse {
String question_1
String question_2
static belongsTo = [user: User]
...
}
If I add belongsTo to QuestionResponse a new column, user_id, gets created in the DB. Now if I run the same code as above, this user_id column has the id populated with that of the current user. However, the relationship table, user_questionresponse is still empty.
I am aware of the approach mentioned by Burt but I assume that should be required only for ManyToMany relationship. If that is required for all relationship, why isn't that the default?
In your first case, you have a OneToMany relationship between User and QuestionResponse with no side being the owner of the relationship. In this case to maintain the relationship between User and QuestionResponse, a third table is required. To persist data you need to do the:
userInstance.addToResponse(new QuestionResponse(question_1: "foo", question_2: "bar")).save(flush: true, failOnError: true)
You are doing user.addToReponse(q) instead it should be user.addToReponse(questionnaire), if it's not a typo and the data is actually not being stored, then check by adding the failOnError parameter to save() method. Sometimes grails save() method fails silently, it should tell you if this is the case.
In second case, you have added the parent to the relationship, so that means you don't need the third table to maintain the relationship. Grails will not create and populate the third table in this case.
The second approach (adding belongsTo in QuestionResponse) seems the right thing to do in your case, since QuestionResponse objects cannot exists without a user and cannot belong to different users.
In that case there's no need to use a third table.
When you run the app for the first time, grails created the relation table (because there was no belongsTo). When you run the app again with belongsTo grails adds the user_id field but DOES NOT DROP the relation table. That's why the table is there and is empty: it's not needed, but grails database auto-update feature only adds things, it does not remove anything.
The same applies to fields: if you remove a field from an entity you have to manually remove it from the database.
I have legacy database and some tables have composite ids
class Client {
String id
static hasMany = [
settings: Setting
]
static mapping = {
id column: 'client_id', generator: 'assigned'
}
}
class Setting {
Client client
String nodeId
String ccyPairPattern
Character qualifier
static mapping = {
id composite: ['client', 'nodeId', 'pattern', 'qualifier']
}
}
I want to delete entry from GORM association:
client.get('1').removeFromSettings(settingToRemove)
// settingToRemove.delete(flush: true)
// delete-orphans does not help
This always raises exception after flush
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) :
This happens because method removeFrom* sets client property to null and generates query to delete with clientId = null as client is part of composite key
What is the best solution in this case. Looks like GORM has poor support for composite keys or my mapping is incorrect.
When you use hasMany without a belongsTo on the many side, in other words a unidirectional association, you get a join table. For example...
class PurchaseOrder {
static hasMany = [items: Item]
}
class Item { }
Would yield three database tables: purchase_order, item, and purchase_order_items. The purchase_order_items table would contain two columns: purchase_order_id and item_id. You can read more about join tables here.
Since you're dealing with a legacy database, I think the best solution is not to use addTo*() and removeFrom*().
Ensure you don't have a join table. If you have a join table, remove the hasMany association.
You'll need to add/remove Setting instances manually.
Example:
def client = Client.get(1)
// Adding a setting
def setting = new Setting(client: client, nodeId: blah...)
setting.save()
// Removing a setting
/*
The prototype is used to search for a domain instance with a composite primary key.
So simply set the composite key properties accordingly.
*/
def prototype = new Setting(client: client, nodeId: blah...)
def setting = Setting.get(prototype)
setting.delete()
Lacking a hasMany association, you won't be able to access a client's settings via the client.settings property. Instead you'd have to query for them like this:
def settings = Setting.findByClient(client)
A consequence of using a legacy database is that if the database doesn't align with that GORM/Hibernate expects it will be limited in what it can do for you.
Suppose I have table Person, student:
Person(id, ....): id is primary key and identity column.
Student(id, .....): id is primary key and foreign key connect to person->ID
In ria service meta data, set Student as composite of Person:
[Include]
[Composition]
public Student Student { get; set; }
then I want to insert a new student data. what I did is something like:
Person p = new person(){....};
p.Student = new Student{....};
_context.People.AddObject(p);
_context.SaveChanges();
then I got error:
System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Student_Person". The conflict occurred in database "MyDB", table "dbo.Person", column 'id'.
The statement has been terminated.
How to fix this problem?
All
My problem is I'm creating a 1-to-many relationship in GORM and expect 2 database tables to be created as backing objects. 3 are created which makes SQL queries overly complex.
I've created a close variant on the 1-to-many in the GORM documentation:
class Status {
List errorMessage
static hasMany = [errorMessage:ErrorMessage]
}
and the error message class:
class ErrorMessage {
String message
static belongsTo = Status
}
I expected this to create two database tables:
CREATE TABLE status {
ID NUMBER(19,0),
VERSION NUMBER(19,0),
//other fields
}
CREATE TABLE error_message {
ID NUMBER(19,0),
VERSION NUMBER(19,0),
STATUS_ID NUMBER(19,0),
MESSAGE VARCHAR(255)
//other fields
}
but actually it wants a third table,
CREATE TABLE status_text {
status_text_id NUMBER(19,0),
text_idx NUMBER(19,0),
text_id NUMBER(19,0)
}
Adding Status to the ErrorMessage (a hack as I don't want ErrorMessage to have a reference to Status) class removes the third table but keeps the second foreign key resulting in the Text child object having two foreign key fields.
What I want is simple - just a set of objects attached to the parent will be deleted when it is - any thoughts what I'm doing wrong?
Thanks
I don't think you can satisfy both requirements, i.e. that you have cascading deletes and no join table. You need the belongsTo to get cascading deletes and that makes the relationship bidirectional. To remove the join table, name the belongsTo with the Map syntax:
class ErrorMessage {
String message
static belongsTo = [status: Status]
}
I have the following domains classes:
class Posts{
String Name
String Country
static hasMany = [tags:Tags]
static constraints = {
}
}
class Tags{
String Name
static belongsTo = Posts
static hasMany = [posts:Posts]
static constraints = {
}
String toString()
{
"${TypeName}"
}
}
Grails creates an another table in the database i.e. Posts_Tags.
My requirement is:
E.g. 1 post has 3 tags.
So, in the Posts_Tags table there are 3 rows.
How can I access the table Posts_Tags directly in my code so that I can manipulate the data or add some more fields to it.
If you want to access the join table (Posts_Tags) directly, or add properties to it, then you must define it as a separate PostTag domain class. You then split your many-many relationship between Post and Tag into 2 one-to-many relationships (one from Post to PostTag and one from Tag to PostTag).
Here's a comprehensive example of how to perform the mapping and add properties to the join table - in this example Membership is the join table.
Use the normal groovy sql API. For an example of how to get a groovy SQL object and execute sql queries see this