Reputation: 15872
Here's a model I have in Persistent:
Tip
text Text
created_at UTCTime
updated_at UTCTime
title Text
category_id CategoryId
And the related ToJSON
instance (I couldn't use deriveJSON
):
instance ToJSON Tip where
toJSON (Tip text created_at updated_at title category_id) = object ["text" .= text, "created_at" .= created_at, "updated_at" .= updated_at, "title" .= title, "category_id" .= category_id]
This is almost correct except I want to JSONify the Tip's id in here too...but it's not anywhere on the model! How would I do this? Is there any way to go from an EntityVal
to a EntityKey
?
Upvotes: 4
Views: 328
Reputation: 71495
An Entity
is the pairing of a key and a value. If you just have the value, there's no generic way to know what id it has. If it has any unique constraints you could use that to look up the key.
A given EntityVal
could theoretically have been assigned different ids in different backend databases (which might not even all have the same type). Or it could have been manually constructed to be inserted but hasn't actually been inserted yet, so it hasn't been assigned an id. A Tip
value doesn't innately have an id; it only maybe has an id in a particular database.
Upvotes: 2