{
  "id": "python/sqlalchemy-circular-import-relationship",
  "signature": "sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Original exception was: ImportError: cannot import name 'Parent' from partially initialized module 'models'",
  "signature_zh": "sqlalchemy.exc.InvalidRequestError: 一个或多个映射器初始化失败 - 无法继续初始化其他映射器。原始异常: ImportError: 无法从部分初始化的模块 'models' 中导入名称 'Parent'",
  "regex": "sqlalchemy\\.exc\\.InvalidRequestError:\\ One\\ or\\ more\\ mappers\\ failed\\ to\\ initialize\\ \\-\\ can't\\ proceed\\ with\\ initialization\\ of\\ other\\ mappers\\.\\ Original\\ exception\\ was:\\ ImportError:\\ cannot\\ import\\ name\\ 'Parent'\\ from\\ partially\\ initialized\\ module\\ 'models'",
  "domain": "python",
  "category": "module_error",
  "subcategory": null,
  "root_cause": "Circular imports between modules when defining relationships, causing the mapper to fail to initialize.",
  "root_cause_type": "generic",
  "root_cause_zh": "定义关系时模块之间的循环导入，导致映射器初始化失败。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Moving all models into one file",
      "why_fails": "This may resolve the import issue but leads to a monolithic, hard-to-maintain codebase.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "Removing the relationship",
      "why_fails": "The ORM functionality is lost.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Use string references in relationships",
      "success_rate": 0.98,
      "how": "class Parent(Base):\n    __tablename__ = 'parents'\n    children = relationship('Child', back_populates='parent')\nclass Child(Base):\n    __tablename__ = 'children'\n    parent_id = Column(Integer, ForeignKey('parents.id'))\n    parent = relationship('Parent', back_populates='children')",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use lazy imports inside the relationship definition",
      "success_rate": 0.9,
      "how": "children = relationship(lambda: Child, back_populates='parent')",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.86,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2025-03-05",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}