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

SET NULL is not set for many-to-many self-relationship tables #430

Closed
ichorid opened this issue Mar 11, 2019 · 1 comment
Closed

SET NULL is not set for many-to-many self-relationship tables #430

ichorid opened this issue Mar 11, 2019 · 1 comment

Comments

@ichorid
Copy link

ichorid commented Mar 11, 2019

While trying to delete an entry with bulk=True, there is a FOREIGN KEY constraint failed message.
Apparently, Pony does not set the SET NULL action in the intermediate table.
That is peculiar because it sets this for regular (not intermediate) tables, as can be seen with
PRAGMA foreign_key_list(db_name);

Minimal example:

from pony import orm
from pony.orm import db_session


db = orm.Database()

class TestClass1(db.Entity):
    rowid = orm.PrimaryKey(int, auto=True)
    friend = orm.Set("TestClass1", reverse="friend")

db.bind(provider='sqlite', filename=":memory:", create_db=True)
db.generate_mapping(create_tables=True)

if __name__ == "__main__":
    with db_session:
        c1 = TestClass1()
        c2 = TestClass1(friend=c1)

    with db_session:
        print TestClass1.select()[:]
        TestClass1.select().delete(bulk=True)

Results in

Traceback (most recent call last):
  File "./pony_bug_self.py", line 21, in <module>
    TestClass1.select().delete(bulk=True)
  File "/home/user/.local/lib/python2.7/site-packages/pony/orm/core.py", line 5922, in delete
    cursor = database._exec_sql(sql, arguments)
  File "/home/user/.local/lib/python2.7/site-packages/pony/orm/core.py", line 941, in _exec_sql
    connection = cache.reconnect(e)
  File "/home/user/.local/lib/python2.7/site-packages/pony/orm/core.py", line 1771, in reconnect
    if not provider.should_reconnect(exc): reraise(*sys.exc_info())
  File "/home/user/.local/lib/python2.7/site-packages/pony/orm/core.py", line 939, in _exec_sql
    try: new_id = provider.execute(cursor, sql, arguments, returning_id)
  File "<auto generated wrapper of execute() function>", line 2, in execute
  File "/home/user/.local/lib/python2.7/site-packages/pony/orm/dbapiprovider.py", line 58, in wrap_dbapi_exceptions
    except dbapi_module.IntegrityError as e: raise IntegrityError(e)
pony.orm.dbapiprovider.IntegrityError: FOREIGN KEY constraint failed
@ichorid
Copy link
Author

ichorid commented Sep 4, 2019

🎉

kozlovsky added a commit that referenced this issue Oct 23, 2019
# Features

* #472: Python 3.8 support
* Support of hybrid functions (inlining simple Python functions into query)
* #438: support datetime-datetime, datetime-timedelta, datetime+timedelta in queries

# Bugfixes

* #430: add ON DELETE CASCADE for many-to-many relationships
* #465: Should reconnect to MySQL on OperationalError 2013 'Lost connection to MySQL server during query'
* #468: Tuple-value comparisons generate incorrect queries
* #470 fix PendingDeprecationWarning of imp module
* Fix incorrect unpickling of objects with Json attributes
* Check value of discriminator column on object creation if set explicitly
* Correctly handle Flask current_user proxy when adding new items to collections
* Some bugs in syntax of aggregated queries were fixed
* Fix syntax of bulk delete queries
* Bulk delete queries should clear query results cache so next select will get correct result from the database
* Fix error message when hybrid method is too complex to decompile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant