[ckan-dev] datastore_upsert error

Adrià Mercader adria.mercader at okfn.org
Thu Jun 2 08:29:38 UTC 2016


Hi Isabel,

Does the actual table on the datastore database has a UNIQUE
constraint created for the poiIDSec field? You can check this running:

sudo -u postgres psql -c '\d "RESOURCE_ID"' datastore_default


The following script works for me:

  import ckanapi

  malaga = ckanapi.RemoteCKAN('http://localhost:5001',
    apikey='API_KEY',
    user_agent='ckanapismassa/1.0')


  rows = [
      {'poiIDSec': '1', 'hola': 'a'},
      {'poiIDSec': '2', 'hola': 'b'},
      {'poiIDSec': '3', 'hola': 'c'},
      {'poiIDSec': '4', 'hola': 'd'},
      {'poiIDSec': '5', 'hola': 'e'},
      {'poiIDSec': '6', 'hola': 'f'},
  ]


  malaga.action.datastore_create(
      resource_id='3165ad32-2e35-46be-aece-01040e59a994',
      force=True,
      primary_key='poiIDSec',
      records=rows
  )

  rows = [
      {'poiIDSec': '1', 'hola': 'a2'},
      {'poiIDSec': '2', 'hola': 'b2'},
      {'poiIDSec': '3', 'hola': 'c2'},
      {'poiIDSec': '4', 'hola': 'd2'},
      {'poiIDSec': '5', 'hola': 'e2'},
      {'poiIDSec': '6', 'hola': 'f2'},
  ]

  malaga.action.datastore_upsert(
      resource_id='3165ad32-2e35-46be-aece-01040e59a994',
      force=True,
      records=rows
  )



On 31 May 2016 at 10:36, Isabel Ruiz <isaruizmellado at gmail.com> wrote:
> Hi all,
>
> I have this problem using datastore API.
>
> I create a datastore and insert rows using this code (I execute this once):
>
> malaga = ckanapi.RemoteCKAN(URL,
>     apikey=APIKEY,
>     user_agent='ckanapismassa/1.0')
>
>   malaga.action.datastore_create(
>     resource_id=RESOURCE_ID,
>     force=True,
>     primary_key='poiIDSec'
>     records=rows
>   )
>
> It works, I mean: it creates a datastore and inserts every rows.
>
> Then, I execute a cron every minutes, that will update rows, this is the
> code:
>
> malaga = ckanapi.RemoteCKAN(URL,
>     apikey=APIKEY,
>     user_agent='ckanapismassa/1.0')
>
>   malaga.action.datastore_upsert(
>     resource_id=RESOURCE_ID,
>     force=True,
>     records=rows
>   )
>
> This returns me this error:
>
> Traceback (most recent call last):
> ValidationError: None - {u'table': [u'table does not have a unique key
> defined'], u'__type': u'Validation Error'}
>
>
> ['Traceback (most recent call last):\n', '  File "<stdin>", line 93, in
> <module>\n', '  File
> "/home/ckan/ckan/lib/default/src/ckanapi/ckanapi/common.py", line 51, in
> action\n    return self._ckan.call_action(name, data_dict=kwargs)\n', '
> File "/home/ckan/ckan/lib/default/src/ckanapi/ckanapi/remoteckan.py", line
> 83, in call_action\n    return reverse_apicontroller_action(url, status,
> response)\n', '  File
> "/home/ckan/ckan/lib/default/src/ckanapi/ckanapi/common.py", line 124, in
> reverse_apicontroller_action\n    raise ValidationError(err)\n',
> "ValidationError: None - {u'table': [u'table does not have a unique key
> defined'], u'__type': u'Validation Error'}\n"]
>
>
> How can I solve this ?
>
> Is there another way to update datastore rows?
>
>
> Thanks!!!
>
> --
> Isabel M. Ruiz Mellado
> http://about.me/isabelruiz
>
> _______________________________________________
> ckan-dev mailing list
> ckan-dev at lists.okfn.org
> https://lists.okfn.org/mailman/listinfo/ckan-dev
> Unsubscribe: https://lists.okfn.org/mailman/options/ckan-dev
>



More information about the ckan-dev mailing list