[ckan-dev] datastore upsert api

Sam Leitner sam.leitner at gmail.com
Wed Jun 24 21:39:13 UTC 2015


Oops, looks like I was confused about key labeling. Sorry for the spam. In
case other people run into similar problems, here's an example bash script
demoing most of the datastore API functionality:

API_KEY='<INSERT YOUR API KEY>'
SITE_API='<CKAN WEBSITE:PORT>/api/3/action/'
RES_ID='<INSERT CREATED RESOURCE ID>'

#create
echo '----------------create'
curl -X POST ${SITE_API}/datastore_create -H "Authorization: ${API_KEY}" -d
'{"force":"true", "resource_id":"'${RES_ID}'", "primary_key":["name"],
"fields": [{"id":"name"},{"id":"tab", "type":"json"}],"records" : [
{"name": "datadict", "tab": [{"item1_int":1,"item2_txt":"xyz"}] } ]}'; echo
'';
curl -X POST ${SITE_API}/datastore_create -H "Authorization: ${API_KEY}" -d
'{"force":"true", "resource_id":"'${RES_ID}'", "records" : [ {"name":
"otherdata", "tab": [{"k1":1,"k2":2}] } ]}'; echo '';

#show
echo '----------------show'
curl -X GET ${SITE_API}/datastore_search?resource_id=${RES_ID} -H
"Authorization: ${API_KEY}"; echo '';

#update
echo '----------------update'
curl -X POST ${SITE_API}/datastore_upsert -H "Authorization: ${API_KEY}" -d
'{"force":"true", "resource_id":"'${RES_ID}'", "method":"upsert", "records"
: [ {"name": "datadict", "tab": [{"item1_int":4444,"item2_txt":1111}] }
]}'; echo '';

#show
echo '----------------show'
curl -X GET ${SITE_API}/datastore_search?resource_id=${RES_ID} -H
"Authorization: ${API_KEY}"; echo '';

#delete
echo '----------------delete otherdata'
curl -X POST ${SITE_API}/datastore_delete -H "Authorization: ${API_KEY}" -d
'{ "force":"true", "resource_id":"'${RES_ID}'",
"filters":{"name":"otherdata"} }'; echo '';
#show
echo '----------------show'
curl -X GET ${SITE_API}/datastore_search?resource_id=${RES_ID} -H
"Authorization: ${API_KEY}"; echo '';
#delete
echo '----------------delete'
curl -X POST ${SITE_API}/datastore_delete -H "Authorization: ${API_KEY}" -d
'{ "force":"true", "resource_id":"'${RES_ID}'", "filters":{} }'; echo '';



On Wed, Jun 24, 2015 at 4:41 PM, Sam Leitner <sam.leitner at gmail.com> wrote:

> Hi all,
>
> I'm having trouble using the datastore_upsert API. I get complaints from
> CKAN about needing a unique key to update:
>
>  [error] [client 10.0.2.2] File
> '/usr/lib/ckan/default/src/ckan/ckanext/datastore/db.py', line 664 in
> upsert_data
> [error] [client 10.0.2.2]   'table': [u'table does not have a unique key
> defined']
> [error] [client 10.0.2.2] ValidationError: None - {'table': [u'table does
> not have a unique key defined']}
>
>  but I don't see a parameter in the docs to produce a unique key when
> creating the datastore table (
> http://docs.ckan.org/en/latest/maintaining/datastore.html#ckanext.datastore.logic.action.datastore_create
> indicates that I can create primary_keys, but then I see "AND
> idx.indisprimary = false" in datastore/db.py).
>
> Here's a gist of some jinja2 code that produces my problem in the app
> (residing in package/snippet/resource_form.html):
> https://gist.github.com/sleitner/2c079e435c04afe52c53
>
> And here's a similar gist of curl commands written into a bash script that
> you can use to reproduce the issue:
> https://gist.github.com/sleitner/e222a0499a8290f64db7
>
> What am I missing?
>
> Thanks for your help!
> Sam
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20150624/3bf813e5/attachment-0003.html>


More information about the ckan-dev mailing list