Issue: how are join keys determined?
Author: ra-esmith
State: closed
Created: 2017-10-16 21:50:59

I am getting the error

17:43:04 web.1   |  ValueError: More than one foreign key between <class 'sample_models.X'> and <class 'sample_models.Y'>. 

How do I determine which keys to join on? 

I think this is the query that is at issue.
In the old peewee I did not specify anyting related to the keys to join on

 (X.select(Y).join(Y).where(...)

What is the correct syntax.  I am using the foreign keys in the where clause ...

Both of my classes X and Y have several foreign keys defined.
How did the last version resolve which ones to use ?

Thanks




[OKAY] Loaded ENV .env File as KEY=VALUE Format
17:41:39 web.1   |  [2017-10-16 17:41:39 -0400] [92954] [INFO] Starting gunicorn 19.7.1
17:41:39 web.1   |  [2017-10-16 17:41:39 -0400] [92954] [INFO] Listening at: http://0.0.0.0:5000 (92954)
17:41:39 web.1   |  [2017-10-16 17:41:39 -0400] [92954] [INFO] Using worker: gevent
17:41:39 web.1   |  [2017-10-16 17:41:39 -0400] [92957] [INFO] Booting worker with pid: 92957
17:41:39 web.1   |  [2017-10-16 17:41:39 -0400] [92958] [INFO] Booting worker with pid: 92958
17:41:39 web.1   |  [2017-10-16 17:41:39 -0400] [92959] [INFO] Booting worker with pid: 92959
17:41:39 web.1   |  [2017-10-16 17:41:39 -0400] [92960] [INFO] Booting worker with pid: 92960
17:43:04 web.1   |  [2017-10-16 17:43:04 -0400] [92959] [ERROR] Error handling request /
17:43:04 web.1   |  Traceback (most recent call last):
17:43:04 web.1   |    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gunicorn/workers/async.py", line 56, in handle
17:43:04 web.1   |      self.handle_request(listener_name, req, client, addr)
17:43:04 web.1   |    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gunicorn/workers/ggevent.py", line 152, in handle_request
17:43:04 web.1   |      super(GeventWorker, self).handle_request(*args)
17:43:04 web.1   |    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gunicorn/workers/async.py", line 107, in handle_request
17:43:04 web.1   |      respiter = self.wsgi(environ, resp.start_response)
17:43:04 web.1   |    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1997, in __call__
17:43:04 web.1   |      return self.wsgi_app(environ, start_response)
17:43:04 web.1   |    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1985, in wsgi_app
17:43:04 web.1   |      response = self.handle_exception(e)
17:43:04 web.1   |    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1540, in handle_exception
17:43:04 web.1   |      reraise(exc_type, exc_value, tb)
17:43:04 web.1   |    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
17:43:04 web.1   |      response = self.full_dispatch_request()
17:43:04 web.1   |    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
17:43:04 web.1   |      rv = self.handle_user_exception(e)
17:43:04 web.1   |    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
17:43:04 web.1   |      reraise(exc_type, exc_value, tb)
17:43:04 web.1   |    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
17:43:04 web.1   |      rv = self.dispatch_request()
17:43:04 web.1   |    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
17:43:04 web.1   |      return self.view_functions[rule.endpoint](**req.view_args)
17:43:04 web.1   |    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask_peewee/auth.py", line 129, in inner
17:43:04 web.1   |      return fn(*args, **kwargs)
17:43:04 web.1   |    File "/Users/anon/Desktop/xxx/sample-master/views/dashboards.py", line 96, in main_page
17:43:04 web.1   |      return get_dashboard_for_y(modelXX.y, user, user.rep_id)
17:43:04 web.1   |    File "/Users/anon/Desktop/xxx/sample-master/views/dashboards.py", line 300, in get_dashboard_for_y
17:43:04 web.1   |      .join(Y)
17:43:04 web.1   |    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/peewee.py", line 595, in inner
17:43:04 web.1   |      method(clone, *args, **kwargs)
17:43:04 web.1   |    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/peewee.py", line 5123, in join
17:43:04 web.1   |      on, attr, constructor = self._normalize_join(src, dest, on, attr)
17:43:04 web.1   |    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/peewee.py", line 5092, in _normalize_join
17:43:04 web.1   |      src_model, dest_model, to_field, on)
17:43:04 web.1   |    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/peewee.py", line 5160, in _generate_on_clause
17:43:04 web.1   |      (src, dest))
17:43:04 web.1   |  ValueError: More than one foreign key between <class 'sample_models.X'> and <class 'sample_models.Y'>. Please specify which you are joining on.
17:43:29 web.1   |  [2017-10-16 17:43:29 -0400] [92954] [INFO] Handling signal: winch
17:43:29 web.1   |  [2017-10-16 17:43:29 -0400] [92954] [INFO] Handling signal: winch
17:43:29 web.1   |  [2017-10-16 17:43:29 -0400] [92954] [INFO] Handling signal: winch
17:43:29 web.1   |  [2017-10-16 17:43:29 -0400] [92954] [INFO] Handling signal: winch
17:43:29 web.1   |  [2017-10-16 17:43:29 -0400] [92954] [INFO] Handling signal: winch
17:43:29 web.1   |  [2017-10-16 17:43:29 -0400] [92954] [INFO] Handling signal: winch
17:43:29 web.1   |  [2017-10-16 17:43:29 -0400] [92954] [INFO] Handling signal: winch
17:43:29 web.1   |  [2017-10-16 17:43:29 -0400] [92954] [INFO] Handling signal: winch
17:43:30 web.1   |  [2017-10-16 17:43:30 -0400] [92954] [INFO] Handling signal: winch
17:43:30 web.1   |  [2017-10-16 17:43:30 -0400] [92954] [INFO] Handling signal: winch
17:43:30 web.1   |  [2017-10-16 17:43:30 -0400] [92954] [INFO] Handling signal: winch
17:43:30 web.1   |  [2017-10-16 17:43:30 -0400] [92954] [INFO] Handling signal: winch
17:43:30 web.1   |  [2017-10-16 17:43:30 -0400] [92954] [INFO] Handling signal: winch
17:43:30 web.1   |  [2017-10-16 17:43:30 -0400] [92954] [INFO] Handling signal: winch


I cannot tell you which foreign key to join on, only you would know that. To specify the key, in the join method add an `on` argument:

```python
Something.select().join(Other, on=Other.some_field)
```