{"action":"created","issue":{"url":"https://api.github.com/repos/jfinkels/flask-restless/issues/200","repository_url":"https://api.github.com/repos/jfinkels/flask-restless","labels_url":"https://api.github.com/repos/jfinkels/flask-restless/issues/200/labels{/name}","comments_url":"https://api.github.com/repos/jfinkels/flask-restless/issues/200/comments","events_url":"https://api.github.com/repos/jfinkels/flask-restless/issues/200/events","html_url":"https://github.com/jfinkels/flask-restless/issues/200","id":14090789,"number":200,"title":"Polymorphism in flask-restless","user":{"login":"gpopovic","id":2094147,"avatar_url":"https://avatars.githubusercontent.com/u/2094147?v=3","gravatar_id":"","url":"https://api.github.com/users/gpopovic","html_url":"https://github.com/gpopovic","followers_url":"https://api.github.com/users/gpopovic/followers","following_url":"https://api.github.com/users/gpopovic/following{/other_user}","gists_url":"https://api.github.com/users/gpopovic/gists{/gist_id}","starred_url":"https://api.github.com/users/gpopovic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gpopovic/subscriptions","organizations_url":"https://api.github.com/users/gpopovic/orgs","repos_url":"https://api.github.com/users/gpopovic/repos","events_url":"https://api.github.com/users/gpopovic/events{/privacy}","received_events_url":"https://api.github.com/users/gpopovic/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/jfinkels/flask-restless/labels/bug","name":"bug","color":"fc2929"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2013-05-08T09:08:58Z","updated_at":"2016-05-11T03:24:46Z","closed_at":null,"body":"I have a SQLAlchemy model named `Version`, and it has a `type` column.\r\nBased on that column I have two polymorphic objects called `Revision` (if `type == \"revision\"`) and `Original` (if `type == \"original\"`). It is polymorphism in the same table.\r\n\r\nWhen you call `create_api()` for objects `Version`, `Revision`, and `Original` they will all use the prefix `/version/`, which is wrong. This happens because they are all using same table.\r\nFlask-restless should a) somehow figure this out, and use model name for API prefix, or b) you should write a note about this in the documentation and tell people to use custom names.\r\nIt took me a while to figure out why i always got an HTTP 404 error when making requests to `/original` and `/revision`.\r\n\r\nI didn't check for polymorphism with different tables but it should probably work.\r\n\r\nI also found another serious error.\r\n\r\n    AttributeError: 'Original' object has no attribute '_sa_polymorphic_on'\r\n\r\nI've fixed this by changing line 246, in file `views.py`, in function `_to_dict` from\r\n\r\n    result = dict((col, getattr(instance, col)) for col in columns) \r\n\r\nto\r\n\r\n    result = dict((col, getattr(instance, col)) for col in columns) if col != \"_sa_polymorphic_on\")\r\n"},"comment":{"url":"https://api.github.com/repos/jfinkels/flask-restless/issues/comments/218352255","html_url":"https://github.com/jfinkels/flask-restless/issues/200#issuecomment-218352255","issue_url":"https://api.github.com/repos/jfinkels/flask-restless/issues/200","id":218352255,"user":{"login":"jfinkels","id":121755,"avatar_url":"https://avatars.githubusercontent.com/u/121755?v=3","gravatar_id":"","url":"https://api.github.com/users/jfinkels","html_url":"https://github.com/jfinkels","followers_url":"https://api.github.com/users/jfinkels/followers","following_url":"https://api.github.com/users/jfinkels/following{/other_user}","gists_url":"https://api.github.com/users/jfinkels/gists{/gist_id}","starred_url":"https://api.github.com/users/jfinkels/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jfinkels/subscriptions","organizations_url":"https://api.github.com/users/jfinkels/orgs","repos_url":"https://api.github.com/users/jfinkels/repos","events_url":"https://api.github.com/users/jfinkels/events{/privacy}","received_events_url":"https://api.github.com/users/jfinkels/received_events","type":"User","site_admin":false},"created_at":"2016-05-11T03:24:46Z","updated_at":"2016-05-11T03:24:46Z","body":"I'm learning about polymorphism in order to fix this bug, and I wonder what you expect the behavior of Flask-Restless to be in the following situation.\r\n\r\nSuppose I have\r\n\r\n```python\r\nversion1 = Original(id=1)\r\nversion2 = Revision(id=2)\r\n```\r\n\r\n(I believe the IDs must be distinct because each object represents a row of the single table `version`.) Now what happens when the client makes the following requests?\r\n\r\n```http\r\nGET /version/1\r\n```\r\n\r\n```http\r\nGET /original/1\r\n```\r\n\r\n```http\r\nGET /revision/1\r\n```\r\n\r\n```http\r\nGET /version/2\r\n```\r\n\r\n```http\r\nGET /original/2\r\n```\r\n\r\n```http\r\nGET /revision/2\r\n```\r\n\r\nThere are two possibilities that I see:\r\n\r\n1. We expect that the only requests that should respond with `200 OK` are `GET /original/1` and `GET /revision/2` and that all others respond with `404 Not Found`.\r\n2. We expect that all requests are OK and none are errors.\r\n\r\nIn either case, we'll have to do some black magic on the model class provided to `APIManager.create_api` to determine whether it is a polymorphic subclass. In the first case, we'll have to do some additional filtering when the query is created on a fetch request.\r\n\r\nI suspect there may be further problems with other types of polymorphism..."}}