Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no foreignAutoDelete?

I'm using Ormlite for Android to convert an existing application from file to database storage of objects. I have a fairly complex object data structure which I need to get into the database and initially at least I don't want to modify too much of the object structure to accomplish this. In this existing structure I have quite a few levels of nested foreign objects.

My question, foreignAutoCreate seems to work pretty well automagically. When I create an object in the database that has foreign objects, that themselves have foreign objects, etc. down to four or five levels, the single create for the master containing object populates all the foreign objects down to all these levels.

I do not believe there is a way to delete the master object and have ormlite cascade the deletes down through all the levels of contained foreign objects. I want to confirm that I'm not missing something and there's not an easier solution for what I'm doing, which is using triggers to delete everything upon a delete from the master table. I understand that cascade deletes are an option, though I could not get it to work. But even if it did work I'm still wondering about the absence of a foreignAutoDelete option as having some of the configuration in the database and some in ormlite seems like it may eventually cause problems.

Right now I'm having problems completing an update option through all these levels of foreign objects. Initially, I was creating the master object and it would create all the children. I would try to update the master object or delete and then readd the master object however it did not seem to impact the children (they were not updated). After adding cascade deletes, I assumed that deleting the master object and cascading through its children and then readding the master object would effectuate the "update", however right now the children aren't being added back when I readd the master object. I'm thinking there's some timing/transaction issue with the delete triggers on child tables happening after adding back the master object. The foreignAutoCreate remains on for all the child objects so I'm assuming they should be populated on the second creation of the master object after everything has been cleared out of the database.

Hope the question makes sense and thanks in advance for any responses.

like image 999
ronbo4610 Avatar asked Mar 08 '13 12:03

ronbo4610


People also ask

Does everyone in China own car?

China has a rather high car ownership rate, mostly because of the status symbol that comes with vehicles. According to the survey conducted by Rakuten Insight, about 71.4 percent of the Chinese respondents stated they owned a car.

Will China export cars?

About. Exports In 2020, China exported $9.22B in Cars, making it the 17th largest exporter of Cars in the world. At the same year, Cars was the 59th most exported product in China.

Can you import cars to China?

Only legal residents of China are permitted to import vehicles, so be sure of your residence status in China. Business or tourist visa holders won't be able to import a car to China, only “Z” visas, or permanent residence cards, are allowed to import a car to China.


1 Answers

Add a columnDefinition parameter to the child's foreign key DatabaseField declaration. In this example document is the parent. DocumentEntity is the name of the class but the table name is defined as "document"

 @DatabaseField(canBeNull = false, foreign = true, index = true, foreignAutoRefresh = true, columnDefinition = "integer references document(id) on delete cascade")
 private DocumentEntity document;
like image 162
RobCroll Avatar answered Sep 25 '22 11:09

RobCroll