AnkiPandas icon indicating copy to clipboard operation
AnkiPandas copied to clipboard

Fix Issue #137: Preserve PRIMARY KEY constraints in write()

Open autoelk opened this issue 3 months ago • 2 comments

When using pandas.to_sql(if_exists="replace"), PRIMARY KEY constraints are lost from SQLite tables. This breaks Anki's synchronization mechanism between devices.

Fix

  • Reassign id_column to be the PRIMARY KEY using dtype={'id_column': 'INTEGER PRIMARY KEY'} parameter when calling pandas.to_sql() in raw.py
  • Added simple regression tests covering both notes and cards tables

Testing

  • All existing tests pass
  • New regression test test_primary_key_constraint_preserved validates the fix

autoelk avatar Sep 24 '25 03:09 autoelk

Review changes with  SemanticDiff

Changed Files
File Status
  ankipandas/raw.py  64% smaller
  ankipandas/test/test_regression.py  0% smaller

semanticdiff-com[bot] avatar Sep 24 '25 03:09 semanticdiff-com[bot]

I want to note that I did not remove the _override_exception check in collection.py so even if this change is merged, the NotImplementedError will still be raised.

autoelk avatar Sep 24 '25 03:09 autoelk