Issue: .get() results in RecursionError
Author: HassanTariqSLX
State: closed
Created: 2018-02-14 06:26:49

Hi,

I'm looping through some adwords MCCs and for each MCC there are customer accounts. If I iterate through these customer accounts and try to retrieve the model, it throws the following:
```
    Traceback (most recent call last):
    File "C:\Users\USERNAME\PycharmProjects\PROJECT_NAME\venv\lib\site-packages\peewee.py", line 5458, in get
        return clone.execute()[0]
      File "C:\Users\USERNAME\PycharmProjects\PROJECT_NAME\venv\lib\site-packages\peewee.py", line 3350, in __getitem__
        return self.row_cache[item]
    IndexError: list index out of range
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "C:/Users/USERNAME/PycharmProjects/PROJECT_NAME/main.py", line 58, in main
        client = AdwordsClient.get(AdwordsClient.adwordsid == customer['customerId'])
      File "C:\Users\USERNAME\PycharmProjects\PROJECT_NAME\venv\lib\site-packages\peewee.py", line 5077, in get
        return sq.get()
      File "C:\Users\USERNAME\PycharmProjects\PROJECT_NAME\venv\lib\site-packages\peewee.py", line 5463, in get
        (clone.model, sql, params))
    database.models.AdwordsClientDoesNotExist: <class 'database.models.AdwordsClient'> instance matching query does not exist:
    SQL: SELECT `t1`.`id`, `t1`.`created`, `t1`.`modified`, `t1`.`name`, `t1`.`adwordsemail`, `t1`.`adwordsid`, `t1`.`adwords_client_status_id`, `t1`.`adwords_sync_status_id`, `t1`.`conversion_tracking`, `t1`.`disapproved_ads` FROM     `adwords_clients` AS `t1` WHERE (`t1`.`adwordsid` = %s) LIMIT 1 OFFSET 0
    Params: ['ADWORD_ID']
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "C:/Users/USERNAME/PycharmProjects/PROJECT_NAME/main.py", line 74, in <module>
        main()
      File "C:/Users/USERNAME/PycharmProjects/PROJECT_NAME/main.py", line 60, in main
        client = AdwordsClient.create(created=datetime.datetime.now())
      File "C:\Users\USERNAME\PycharmProjects\PROJECT_NAME\venv\lib\site-packages\peewee.py", line 5063, in create
        inst.save(force_insert=True)
      File "C:\Users\USERNAME\PycharmProjects\PROJECT_NAME\venv\lib\site-packages\peewee.py", line 5163, in save
        pk_value = self._pk
      File "C:\Users\USERNAME\PycharmProjects\PROJECT_NAME\venv\lib\site-packages\peewee.py", line 5128, in get_id
        return getattr(self, self._meta.primary_key.name)
      File "C:\Users\USERNAME\PycharmProjects\PROJECT_NAME\venv\lib\site-packages\peewee.py", line 3521, in __get__
        .where(self.field == getattr(instance, dest)))
      File "C:\Users\USERNAME\PycharmProjects\PROJECT_NAME\venv\lib\site-packages\peewee.py", line 3521, in __get__
        .where(self.field == getattr(instance, dest)))
      File "C:\Users\USERNAME\PycharmProjects\PROJECT_NAME\venv\lib\site-packages\peewee.py", line 3521, in __get__
        .where(self.field == getattr(instance, dest)))
      [Previous line repeated 483 more times]
      File "C:\Users\USERNAME\PycharmProjects\PROJECT_NAME\venv\lib\site-packages\peewee.py", line 3519, in __get__
        return (self.model
      File "C:\Users\USERNAME\PycharmProjects\PROJECT_NAME\venv\lib\site-packages\peewee.py", line 4997, in select
        return ModelSelect(cls, fields, is_default=is_default)
      File "C:\Users\USERNAME\PycharmProjects\PROJECT_NAME\venv\lib\site-packages\peewee.py", line 5504, in __init__
        super(ModelSelect, self).__init__([model], fields)
      File "C:\Users\USERNAME\PycharmProjects\PROJECT_NAME\venv\lib\site-packages\peewee.py", line 5393, in __init__
        super(_ModelQueryHelper, self).__init__(*args, **kwargs)
      File "C:\Users\USERNAME\PycharmProjects\PROJECT_NAME\venv\lib\site-packages\peewee.py", line 1766, in __init__
        super(Select, self).__init__(**kwargs)
      File "C:\Users\USERNAME\PycharmProjects\PROJECT_NAME\venv\lib\site-packages\peewee.py", line 652, in __init__
        super(_HashableSource, self).__init__(*args, **kwargs)
      File "C:\Users\USERNAME\PycharmProjects\PROJECT_NAME\venv\lib\site-packages\peewee.py", line 614, in __init__
        super(Source, self).__init__()
      File "C:\Users\USERNAME\PycharmProjects\PROJECT_NAME\venv\lib\site-packages\peewee.py", line 1579, in __init__
        super(Query, self).__init__(**kwargs)
    RecursionError: maximum recursion depth exceeded
    
    Process finished with exit code 1
```
I've tried setting the recursion limit using `sys.setrecursionlimit()` but the script just crashes.

Here's the code that causes the exception:

```python
    try:

        client = AdwordsClient.get(AdwordsClient.adwordsid == customer['customerId'])

    except peewee.DoesNotExist:

        client = AdwordsClient.create(created=datetime.datetime.now())
```
I can do other operations without RecursionError just fine.

Any help would be much appreciated.

Can you share the model definition with me? Is the primary key of `AdwordsClient` a foreign-key to some other model?

The recursion error is occurring during the save operation when you're creating the new `client` instance. It's attempting to access the model class' `_pk` property, which obtains the value of the primary key. It seems that this is triggering a foreign-key lookup which is self-referential.

@coleifer Yeah, that was the issue. The foreign relation was configured incorrectly. Thanks a lot for your help.