Issue: TypeError: int() argument must be a string, a bytes-like object or a number when importing models
Author: koadjunky
State: closed
Created: 2021-12-02 18:33:08

Python: 3.8.11
peewee: 3.14.8

I have two files with two models connected with foreign key: factory.py and planet.py. Each file has identical test code (except imports). Calling 'python dsp_be/logic/factory.py' works. Calling 'python dsp_be/logic/planet.py' throws an exception:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'Planet'

Minimal code demonstrating the issue attached. Add "." to PYTHONPATH before executing.
[PeeWee-error.zip](https://github.com/coleifer/peewee/files/7644256/PeeWee-error.zip)

The Internet is silent about the issue, I only found this https://stackoverflow.com/questions/26181642/peewee-foreignkeyfield-doesnt-accept-appropriate-foreign-key-field but the solution is not working for me (outdated?). I'll be grateful for any hint.

I'm not going to download or run that code, but you are welcome to paste a short snippet demonstrating the issue - or even just the actual traceback...

```
(PeeWee-error) maciej@pippin:~/PeeWee-error$ python dsp_be/logic/planet.py 
Traceback (most recent call last):
  File "dsp_be/logic/planet.py", line 14, in <module>
    factory = Factory.create(planet=earth)
  File "/home/maciej/.pyenv/versions/PeeWee-error/lib/python3.8/site-packages/peewee.py", line 6393, in create
    inst.save(force_insert=True)
  File "/home/maciej/.pyenv/versions/PeeWee-error/lib/python3.8/site-packages/peewee.py", line 6603, in save
    pk = self.insert(**field_dict).execute()
  File "/home/maciej/.pyenv/versions/PeeWee-error/lib/python3.8/site-packages/peewee.py", line 1911, in inner
    return method(self, database, *args, **kwargs)
  File "/home/maciej/.pyenv/versions/PeeWee-error/lib/python3.8/site-packages/peewee.py", line 1982, in execute
    return self._execute(database)
  File "/home/maciej/.pyenv/versions/PeeWee-error/lib/python3.8/site-packages/peewee.py", line 2761, in _execute
    return super(Insert, self)._execute(database)
  File "/home/maciej/.pyenv/versions/PeeWee-error/lib/python3.8/site-packages/peewee.py", line 2479, in _execute
    cursor = database.execute(self)
  File "/home/maciej/.pyenv/versions/PeeWee-error/lib/python3.8/site-packages/peewee.py", line 3172, in execute
    sql, params = ctx.sql(query).query()
  File "/home/maciej/.pyenv/versions/PeeWee-error/lib/python3.8/site-packages/peewee.py", line 614, in sql
    return obj.__sql__(self)
  File "/home/maciej/.pyenv/versions/PeeWee-error/lib/python3.8/site-packages/peewee.py", line 2738, in __sql__
    self._simple_insert(ctx)
  File "/home/maciej/.pyenv/versions/PeeWee-error/lib/python3.8/site-packages/peewee.py", line 2588, in _simple_insert
    return self._generate_insert((self._insert,), ctx)
  File "/home/maciej/.pyenv/versions/PeeWee-error/lib/python3.8/site-packages/peewee.py", line 2710, in _generate_insert
    return ctx.sql(CommaNodeList(all_values))
  File "/home/maciej/.pyenv/versions/PeeWee-error/lib/python3.8/site-packages/peewee.py", line 614, in sql
    return obj.__sql__(self)
  File "/home/maciej/.pyenv/versions/PeeWee-error/lib/python3.8/site-packages/peewee.py", line 1777, in __sql__
    ctx.sql(self.nodes[n_nodes - 1])
  File "/home/maciej/.pyenv/versions/PeeWee-error/lib/python3.8/site-packages/peewee.py", line 614, in sql
    return obj.__sql__(self)
  File "/home/maciej/.pyenv/versions/PeeWee-error/lib/python3.8/site-packages/peewee.py", line 1777, in __sql__
    ctx.sql(self.nodes[n_nodes - 1])
  File "/home/maciej/.pyenv/versions/PeeWee-error/lib/python3.8/site-packages/peewee.py", line 614, in sql
    return obj.__sql__(self)
  File "/home/maciej/.pyenv/versions/PeeWee-error/lib/python3.8/site-packages/peewee.py", line 1383, in __sql__
    return ctx.value(self.value, self.converter)
  File "/home/maciej/.pyenv/versions/PeeWee-error/lib/python3.8/site-packages/peewee.py", line 626, in value
    value = converter(value)
  File "/home/maciej/.pyenv/versions/PeeWee-error/lib/python3.8/site-packages/peewee.py", line 5242, in db_value
    return self.rel_field.db_value(value)
  File "/home/maciej/.pyenv/versions/PeeWee-error/lib/python3.8/site-packages/peewee.py", line 4569, in db_value
    return value if value is None else self.adapt(value)
  File "/home/maciej/.pyenv/versions/PeeWee-error/lib/python3.8/site-packages/peewee.py", line 4629, in adapt
    return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Planet'
```

Looks like you have code here on line 14:

```python
factory = Factory.create(planet=earth)
```

The definition of `Factory.planet` (I'm guessing) is not a `ForeignKeyField` perhaps, when it should be. Possibly you put it as an IntegerField or something?

It only happens if models are in different files. Merge it into one - and it works.

```
(PeeWee-error) maciej@pippin:~/PeeWee-error$ cat dsp_be/logic/__init__.py 
from typing import List, Type

from peewee import DatabaseProxy, SqliteDatabase, Model

db_proxy = DatabaseProxy()


class BaseModel(Model):
    class Meta:
        database = db_proxy


def test_database(tables: List[Type[Model]]):
    db = SqliteDatabase(':memory:')
    db_proxy.initialize(db)
    db_proxy.connect()
    db_proxy.create_tables(tables)
```

```
(PeeWee-error) maciej@pippin:~/PeeWee-error$ cat dsp_be/logic/planet.py 
from peewee import Field, CharField

from dsp_be.logic import test_database, BaseModel


class Planet(BaseModel):
    name: Field = CharField()


if __name__ == '__main__':
    from dsp_be.logic.factory import Factory
    test_database([Planet, Factory])
    earth = Planet.create(name='Earth')
    factory = Factory.create(planet=earth)
```

```
(PeeWee-error) maciej@pippin:~/PeeWee-error$ cat dsp_be/logic/factory.py 
from peewee import Field, ForeignKeyField

from dsp_be.logic import test_database, BaseModel
from dsp_be.logic.planet import Planet


class Factory(BaseModel):
    planet: Field = ForeignKeyField(Planet, backref="planets")


if __name__ == '__main__':
    test_database([Planet, Factory])
    earth = Planet.create(name='Earth')
    factory = Factory.create(planet=earth)
```

You've got some bug in your code. This works fine for me:

```python
# testx/__init__.py
from peewee import *

db = DatabaseProxy()

class BaseModel(Model):
    class Meta:
        database = db

# testx/factory.py
from peewee import ForeignKeyField
from testx import BaseModel
from testx.planet import Planet

class Factory(BaseModel):
    planet = ForeignKeyField(Planet)

# testx/planet.py
from peewee import TextField
from testx import BaseModel

class Planet(BaseModel):
    name = TextField()

# testx/main.py
from peewee import *
from testx import db
from testx.planet import Planet
from testx.factory import Factory


_db = SqliteDatabase(':memory:')
db.initialize(_db)
db.create_tables([Factory, Planet])

p = Planet.create(name='earth')
Factory.create(planet=p)
print(Factory.get().planet.name)  # "earth"
```

I copied part of planet.py into main.py similar to your snippet and now it works. Weird.
```
# dsp_be/logic/main.py
from peewee import SqliteDatabase

from dsp_be.logic import test_database


if __name__ == '__main__':
    from dsp_be.logic.factory import Factory, Planet
    test_database([Factory, Planet])
    earth = Planet.create(name='Earth')
    factory = Factory.create(planet=earth)
```
Can be used as an workaround through. Thanks for hint!