Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cascade delete error: FOREIGN KEY constraint failed, with complex entity relationships #278

andreymal opened this issue Jul 13, 2017 · 2 comments


Copy link

from pony.orm import * 

db = Database()

class Author(db.Entity):
    post = Optional('Post', cascade_delete=True)
    edits = Set('PostEdit')

class Post(db.Entity):
    author = Required(Author)
    edits = Set('PostEdit')

class PostEdit(db.Entity):
    editor = Required(Author)
    post = Optional(Post)

db.bind('sqlite', ':memory:')

# fill database
with db_session:
    Post(author=1, id=1)
    PostEdit(id=1, editor=1, post=1)

# and crash it
with db_session:
Traceback (most recent call last):
  File "", line 33, in <module>
  File "/home/andreymal/.local/lib/python3.6/site-packages/pony/orm/", line 394, in __exit__
  File "/home/andreymal/.local/lib/python3.6/site-packages/pony/orm/", line 52, in wrap_dbapi_exceptions
    except dbapi_module.IntegrityError as e: raise IntegrityError(e)
pony.orm.dbapiprovider.IntegrityError: FOREIGN KEY constraint failed
@sashaaero sashaaero added the bug label Sep 19, 2017
Copy link

sashaaero commented Sep 19, 2017

Hello! Yes, this is the bug that we will fix soon.
For now to get this code works you can do this:

# and crash it
with db_session:
    a = Author.get(id=1)
    a.edits.clear() # <- Add this line. This is the problem actually

@kozlovsky kozlovsky added this to the 0.7.3 milestone Sep 20, 2017
Copy link


kozlovsky added a commit that referenced this issue Oct 23, 2017
# New features

* `where()` method added to query
* `coalesce()` function added
* `between(x, a, b)` function added
* #295: Add `_table_options_` for entity class to specify engine, tablespace, etc.
* Make debug flag thread-local
* `sql_debugging` context manager added
* `sql_debug` and show_values arguments to db_session added
* `set_sql_debug` function added as alias to (to be deprecated) `sql_debug` function
* Allow `db_session` to accept `ddl` parameter when used as context manager
* Add `optimistic=True` option to db_session
* Skip optimistic checks for queries in `db_session` with `serializable=True`
* `fk_name` option added for attributes in order to specify foreign key name
* #280: Now it's possible to specify `timeout` option, as well as pass other keyword arguments for `sqlite3.connect` function
* Add support of explicit casting to int in queries using `int()` function
* Added modulo division % native support in queries

# Bugfixes

* Fix bugs with composite table names
* Fix invalid foreign key & index names for tables which names include schema name
* For queries like `select(x for x in MyObject if not x.description)` add "OR IS NULL" for nullable string columns
* Add optimistic checking for `delete()` method
* Show updated attributes when `OptimisticCheckError` is being raised
* Fix incorrect aliases in nested queries
* Correctly pass exception from user-defined functions in SQLite
* More clear error messages for `UnrepeatableReadError`
* Fix `db_session(strict=True)` which was broken in 2d3afb2
* Fixes #170: Problem with a primary key column used as a part of another key
* Fixes #223: incorrect result of `getattr(entity, attrname)` when the same lambda applies to different entities
* Fixes #266: Add handler to `"pony.orm"` logger does not work
* Fixes #278: Cascade delete error: FOREIGN KEY constraint failed, with complex entity relationships
* Fixes #283: Lost Json update immediately after object creation
* Fixes #284: `query.order_by()` orders Json numbers like strings
* Fixes #288: Expression text parsing issue in Python 3
* Fixes #293: translation of if-expressions in expression
* Fixes #294: Real stack traces swallowed within IPython shell
* `Collection.count()` method should check if session is alive
* Set `obj._session_cache_` to None after exiting from db session for better garbage collection
* Unload collections which are not fully loaded after exiting from db session for better garbage collection
* Raise on unknown options for attributes that are part of relationship
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

3 participants