According to http://www.yesodweb.com/blog/2010/07/database-migrations a DeleteCascade
typeclass was added a few years ago. I can only assume that this is meant to be added to the models. After setting up my models config like this:
Field
...
foreignId ForeignId DeleteCascade
my application compiles fine. but the DB schema is unchanged, and the delete is not cascaded by the application either. Should I just (shudder) do it manually? Is there a better way?
I'm using the Yesod scaffold (Application.hs, Foundation.hs, Settings.hs, ...)
Using the existing answers, it took some time for me to figure out how to use the delete cascade. As a beginner in Yesod, I did not have an overview over Yesod.
To get a working delete cascade you don't need to change anything in the model. Let's say your entities file looks like the following and you want to delete the Entry
.
Entry
title Text
Comment
entry EntryId
You do not have to change this entity file.
The code from where you lead the entities, typically in Model.hs
, looks like that.
share [ mkPersist sqlOnlySettings
, mkMigrate "migrateAll" ]
$(persistFileWith lowerCaseSettings "config/models")
Add the mkDeleteCascade to get the DeleteCascades instances for your entities.
share [ mkPersist sqlOnlySettings
, mkDeleteCascade sqlOnlySettings
, mkMigrate "migrateAll" ]
$(persistFileWith lowerCaseSettings "config/models")
Once you want to delete your entry, for example in the postDeleteEntryR
Handler you have to use deleteCascade
or deleteCascadeWhere
instead of delete.
runDB $ deleteCascade entryId
Using delete
has the same effect as before.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With