~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(models): use_alter on ResearchResource.document_id to break circular FK (#3776)
LearningCircuitbc6a11800f57
diff --git a/src/local_deep_research/database/models/research.py b/src/local_deep_research/database/models/research.pyindex f37588ca88..17688ead0b 100644--- a/src/local_deep_research/database/models/research.py+++ b/src/local_deep_research/database/models/research.py@@ -203,9 +203,24 @@ class ResearchResource(Base):source_type = Column(Text)resource_metadata = Column("metadata", JSON)created_at = Column(String, nullable=False)+ # `use_alter=True` breaks the model-level circular FK with Document+ # (Document.resource_id → research_resources.id, ResearchResource.document_id+ # → documents.id). Without it, every cold start emits an SAWarning from+ # `Base.metadata.sorted_tables` because SQLAlchemy can't topologically+ # order the two tables. With `use_alter=True`, this FK is emitted as a+ # post-CREATE ALTER TABLE so both tables exist by the time the+ # constraint is added. Migration 0005 already chose not to enforce+ # this FK at the DB level on existing DBs (SQLite batch-alter+ # limitation), so this only changes how `create_all()` emits the FK+ # for fresh installs going forward.document_id = Column(String(36),- ForeignKey("documents.id", ondelete="SET NULL"),+ ForeignKey(+ "documents.id",+ ondelete="SET NULL",+ use_alter=True,+ name="fk_research_resources_document_id",+ ),nullable=True,index=True,)diff --git a/tests/database/test_alembic_migrations.py b/tests/database/test_alembic_migrations.pyindex 52b3eced88..8889b6b7c6 100644--- a/tests/database/test_alembic_migrations.py+++ b/tests/database/test_alembic_migrations.py@@ -5859,6 +5859,39 @@ def _get_migration_stems():_MIGRATION_STEMS = _get_migration_stems()+class TestNoCircularFkSawarning:+ """Regression: the documents ↔ research_resources circular FK used to+ emit a SAWarning from Base.metadata.sorted_tables on every cold start.++ Fixed by adding `use_alter=True` to ResearchResource.document_id so+ SQLAlchemy emits that one FK as a post-CREATE ALTER TABLE (breaking+ the dependency cycle for sorting purposes) while still creating the+ constraint at the database level.+ """++ def test_create_all_emits_no_circular_fk_warning(self, tmp_path):+ import warnings as _warnings++ engine = create_engine(f"sqlite:///{tmp_path}/test.db")+ try:+ with _warnings.catch_warnings(record=True) as caught:+ _warnings.simplefilter("always")+ Base.metadata.create_all(engine)+ offending = [+ w+ for w in caught+ if "unresolvable cycles" in str(w.message)+ and "documents" in str(w.message)+ and "research_resources" in str(w.message)+ ]+ assert not offending, (+ f"create_all() emitted the circular-FK SAWarning: "+ f"{[str(w.message) for w in offending]}"+ )+ finally:+ engine.dispose()++class TestMigrationSafetyGuards:"""Guards that catch common migration pitfalls.