pyeve / eve-sqlalchemy

SQLAlchemy data layer for Eve-powered RESTful APIs
http://eve-sqlalchemy.readthedocs.io
Other
232 stars 70 forks source link

Error with Oracle connection #42

Closed samtux closed 9 years ago

samtux commented 9 years ago

I have configured the REST API with a connection to Oracle:

https://gist.github.com/samtux/e7f85eb6bad8e3ea5b7d

When I request all records as:

curl http://localhost:5000/pregunta
curl http://localhost:5000/tercero

Returns all records correctly. But when I ask a single record returns an error, such as:

curl http://localhost:5000/pregunta/25
<!--

Traceback (most recent call last):
  File "/home/visorgeografico/.virtualenvs/ontrackVirtual/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/visorgeografico/.virtualenvs/ontrackVirtual/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/home/visorgeografico/.virtualenvs/ontrackVirtual/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/visorgeografico/.virtualenvs/ontrackVirtual/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/visorgeografico/.virtualenvs/ontrackVirtual/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/visorgeografico/.virtualenvs/ontrackVirtual/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/visorgeografico/.virtualenvs/ontrackVirtual/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/visorgeografico/.virtualenvs/ontrackVirtual/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/visorgeografico/.virtualenvs/ontrackVirtual/lib/python2.7/site-packages/eve/endpoints.py", line 90, in item_endpoint
    response = getitem(resource, **lookup)
  File "/home/visorgeografico/.virtualenvs/ontrackVirtual/lib/python2.7/site-packages/eve/methods/common.py", line 239, in rate_limited
    return f(*args, **kwargs)
  File "/home/visorgeografico/.virtualenvs/ontrackVirtual/lib/python2.7/site-packages/eve/auth.py", line 68, in decorated
    return f(*args, **kwargs)
  File "/home/visorgeografico/.virtualenvs/ontrackVirtual/lib/python2.7/site-packages/eve/methods/common.py", line 820, in decorated
    r = f(resource, **combined_args)
  File "/home/visorgeografico/.virtualenvs/ontrackVirtual/lib/python2.7/site-packages/eve/methods/get.py", line 295, in getitem
    response[config.ID_FIELD])
KeyError: '_id'

-->
kiniou commented 9 years ago

As per your gist, your Pregnunta model is defined with an id column however Eve defaults to _id. You should try to set the ID_FIELD to id in your settings and see if it solves your issue.

samtux commented 9 years ago

Thanks @kiniou, I updated the settings[1] and added the line "ID_FIELD: 'id'" as you suggest [2]. However I have two tables with different column name primary key in this case PREGUNTA with column primary key name 'id' and TERCERO with column named primary key 'numter'. It is possible to have different ID_FIELD depending on the column primary key name of each table?.

For the above settings works properly:

http://0.0.0.0:5000/pregunta/25

But it does not work for the next case column name 'numter':

http http://0.0.0.0:5000/tercero/19496819

I really want to congratulate you for this great software, service response time is very fast and its configuration is very easy.

[1] https://gist.github.com/samtux/e7f85eb6bad8e3ea5b7d#file-ontrack_rest-py-L37 [2] https://github.com/RedTurtle/eve-sqlalchemy/blob/5b5d6399bd241e96ee9fec7bb19e38d4550167f9/docs/tutorial.rst#how-to-adjust-the-primary-column-name

kiniou commented 9 years ago

After testing the trivial.py example, i found that after putting ID_FIELD: 'id' in the settings doesn't work either and i think there might be a race condition between models registration in the domain and application initialization.

Actually, the register_schema is using the Eve's default settings for the item_lookup_field because the app.config is not yet initialized.

Your first query is working since you override the item_lookup_field in the corresponding domain entry for Pregunta. You should do the same for your Tercero domain by using item_lookup_field:numter.

You can also take a look at the hybrid_property method which may be used to declare some id attributes which will point to a different column name(cf. examples/tables.py). But this may be considered as hacky way to do it :smile:.

PS: You should congratulate @amleczko for Eve-SQLAlchemy and @nicolaiarocci for Eve. I'm only some modest contributors among others :wink:.