Issue: RecursionError: maximum recursion depth exceeded while calling a Python object
Author: tekumara
State: closed
Created: 2022-02-13 10:22:15

When two tables have foreign keys that reference each other:

```
Traceback (most recent call last):
  File "/Users/tekumara/.local/bin/sqlite_web", line 8, in <module>
    sys.exit(main())
  File "/Users/tekumara/.local/pipx/venvs/sqlite-web/lib/python3.7/site-packages/sqlite_web/sqlite_web.py", line 841, in main
    initialize_app(args[0], options.read_only, password, options.url_prefix)
  File "/Users/tekumara/.local/pipx/venvs/sqlite-web/lib/python3.7/site-packages/sqlite_web/sqlite_web.py", line 812, in initialize_app
    dataset = SqliteDataSet('sqlite:///%s' % filename, bare_fields=True)
  File "/Users/tekumara/.local/pipx/venvs/sqlite-web/lib/python3.7/site-packages/playhouse/dataset.py", line 48, in __init__
    **kwargs)
  File "/Users/tekumara/.local/pipx/venvs/sqlite-web/lib/python3.7/site-packages/playhouse/reflection.py", line 764, in generate_models
    _create_model(table, models)
  File "/Users/tekumara/.local/pipx/venvs/sqlite-web/lib/python3.7/site-packages/playhouse/reflection.py", line 688, in _create_model
    _create_model(dest, models)
  File "/Users/tekumara/.local/pipx/venvs/sqlite-web/lib/python3.7/site-packages/playhouse/reflection.py", line 688, in _create_model
    _create_model(dest, models)
  File "/Users/tekumara/.local/pipx/venvs/sqlite-web/lib/python3.7/site-packages/playhouse/reflection.py", line 688, in _create_model
    _create_model(dest, models)
  [Previous line repeated 989 more times]
  File "/Users/tekumara/.local/pipx/venvs/sqlite-web/lib/python3.7/site-packages/playhouse/reflection.py", line 684, in _create_model
    for foreign_key in database.foreign_keys[table]:
RecursionError: maximum recursion depth exceeded while calling a Python object
```

peewee 3.14.8

Can you please share the schema (which can be dumped from the sqlite3 utility)?

```
echo ".schema" | sqlite3 my_db.db > schema.sql
```

I didn't believe it was possible to have circular foreign-key dependencies with Sqlite, so I'm not sure how to replicate this.

```
CREATE TABLE flow_run_state (
	id CHAR(36) NOT NULL,
	flow_run_id CHAR(36) NOT NULL,
	CONSTRAINT pk_flow_run_state PRIMARY KEY (id),
	CONSTRAINT fk_flow_run_state__flow_run_id__flow_run FOREIGN KEY(flow_run_id) REFERENCES flow_run (id) ON DELETE cascade
);

CREATE TABLE flow_run (
	id CHAR(36) NOT NULL,
	state_id CHAR(36) NOT NULL,
	CONSTRAINT pk_flow_run PRIMARY KEY (id),
	CONSTRAINT fk_flow_run__state_id__flow_run_state FOREIGN KEY(state_id) REFERENCES flow_run_state (id) ON DELETE SET NULL
);
```

Is that even a valid schema in Sqlite? I suppose it probably works if you disable foreign-key validation (pragma foreign_keys=0) but if foreign-keys are enabled I was unable to insert into either table. Furthermore you have an ON DELETE SET NULL instruction on a non-nullable column. What is the provenance of this schema? Seems broken in several ways, to be honest.

Thank you for providing some sql to replicate the issue, nonetheless.

The code will now drop a warning if it encounters a circular dependency, but should proceed and generate the correct(ish) code (excluding issues with the validity of the schema overall).

Thanks @coleifer!

The schema above is a minimal example (irrelevant fields are removed) I derived from the Prefect orion database, see [here](https://github.com/PrefectHQ/prefect/blob/7562a89/src/prefect/orion/database/orm_models.py#L396)

Yeah, I figured it was something like that. There's never a good reason to be using a cyclical foreign-key, but it does happen so we might as well support it.

There's a new peewee (3.14.9) on pypi now that contains the fix.

Fantastic thank you for the fast fix @coleifer I can confirm it works! 🙌