Issue: AttributeError: 'BackrefAccessor' object has no attribute 'replace'
Author: smartstoic
State: closed
Created: 2020-03-30 12:23:54

Hello,

Thanks a lot for the amazing library.

I have the following issue:

```
import json
from peewee import *

def update_exchanges():
    text = '{"exchange": "ADS","region": "AE","description": "Abu Dhabi Securities Exchange","mic": "XADS", "exchange_suffix": "-DH"}'
    j = json.loads(text)
    Exchanges.insert(j).on_conflict(conflict_target=Exchanges.exchange, preserve=[Exchanges.exchange]).execute()

class Exchanges(Model):
    exchange = TextField(primary_key=True)
    region = TextField(null=True)
    description = TextField(null=True)
    mic = TextField(null=True)
    exchange_suffix = TextField(null=True)

class Symbols(Model):
    symbol = TextField(primary_key=True)
    exchange = ForeignKeyField(Exchanges, backref='exchange', db_column='exchange')

db = PostgresqlDatabase('test', user='postgres', password='tr1234', host='localhost', port=5432)
db.bind([Exchanges])
db.drop_tables([Exchanges])
db.create_tables([Exchanges])

update_exchanges()
```

The above fails with:

```
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.1\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.1\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/Ioannis/.PyCharmCE2019.2/config/scratches/scratch_23.py", line 31, in <module>
    update_exchanges()
  File "C:/Users/Ioannis/.PyCharmCE2019.2/config/scratches/scratch_23.py", line 13, in update_exchanges
    Exchanges.insert(j).on_conflict(conflict_target=Exchanges.exchange, preserve=[Exchanges.exchange]).execute()
  File "D:\Projects\Trader\venv\lib\site-packages\peewee.py", line 1877, in inner
    return method(self, database, *args, **kwargs)
  File "D:\Projects\Trader\venv\lib\site-packages\peewee.py", line 1948, in execute
    return self._execute(database)
  File "D:\Projects\Trader\venv\lib\site-packages\peewee.py", line 2698, in _execute
    return super(Insert, self)._execute(database)
  File "D:\Projects\Trader\venv\lib\site-packages\peewee.py", line 2431, in _execute
    cursor = self.execute_returning(database)
  File "D:\Projects\Trader\venv\lib\site-packages\peewee.py", line 2438, in execute_returning
    cursor = database.execute(self)
  File "D:\Projects\Trader\venv\lib\site-packages\peewee.py", line 3102, in execute
    sql, params = ctx.sql(query).query()
  File "D:\Projects\Trader\venv\lib\site-packages\peewee.py", line 606, in sql
    return obj.__sql__(self)
  File "D:\Projects\Trader\venv\lib\site-packages\peewee.py", line 2687, in __sql__
    update = self._on_conflict.get_conflict_update(ctx, self)
  File "D:\Projects\Trader\venv\lib\site-packages\peewee.py", line 1831, in get_conflict_update
    return ctx.state.conflict_update(self, query)
  File "D:\Projects\Trader\venv\lib\site-packages\peewee.py", line 3862, in conflict_update
    return self._build_on_conflict_update(oc, query)
  File "D:\Projects\Trader\venv\lib\site-packages\peewee.py", line 3150, in _build_on_conflict_update
    excluded = NodeList((SQL('EXCLUDED'), ensure_entity(column)),
  File "D:\Projects\Trader\venv\lib\site-packages\peewee.py", line 384, in ensure_entity
    return value if isinstance(value, Node) else Entity(value)
  File "D:\Projects\Trader\venv\lib\site-packages\peewee.py", line 1466, in __init__
    self._path = [part.replace('"', '""') for part in path if part]
  File "D:\Projects\Trader\venv\lib\site-packages\peewee.py", line 1466, in <listcomp>
    self._path = [part.replace('"', '""') for part in path if part]
AttributeError: 'BackrefAccessor' object has no attribute 'replace'
```
If I remove the Symbols model, then the operation is successful. It looks like the backref in the Symbols model is causing the problem.

Python 3.7.1 (default, Oct 28 2018, 08:39:03) [MSC v.1912 64 bit (AMD64)] on win32
Latest Postgres 12.2

Any help is appreciated.

Best regards


Your `Symbol` exchange backref is stomping on your `Exchanges.exchange` field. Try renaming it to something more semantic:

```python

class Symbols(Model):
    symbol = TextField(primary_key=True)
    # backref changed to "symbols", which is more accurate anyways.
    exchange = ForeignKeyField(Exchanges, backref='symbols', db_column='exchange')
```

Thank you Sir ;)