{
  "id": "python/sqlalchemy-many-to-many-relationship-config",
  "signature": "sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with the operation. Original exception was: For relationship 'User.roles', the secondary table 'user_roles' is not in the same MetaData as the parent and child tables.",
  "signature_zh": "一个或多个映射器初始化失败 - 无法继续操作。原始异常：对于关系 'User.roles'，辅助表 'user_roles' 与父表和子表不在同一个 MetaData 中。",
  "regex": "sqlalchemy\\.exc\\.InvalidRequestError:\\ One\\ or\\ more\\ mappers\\ failed\\ to\\ initialize\\ \\-\\ can't\\ proceed\\ with\\ the\\ operation\\.\\ Original\\ exception\\ was:\\ For\\ relationship\\ 'User\\.roles',\\ the\\ secondary\\ table\\ 'user_roles'\\ is\\ not\\ in\\ the\\ same\\ MetaData\\ as\\ the\\ parent\\ and\\ child\\ tables\\.",
  "domain": "python",
  "category": "config_error",
  "subcategory": null,
  "root_cause": "The association table used in a many-to-many relationship is defined with a different MetaData instance or not properly shared.",
  "root_cause_type": "generic",
  "root_cause_zh": "多对多关系中使用的关联表使用不同的 MetaData 实例定义或未正确共享。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Causes table redefinition and still may not share MetaData.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "SQLAlchemy requires consistent metadata for relationships.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "from sqlalchemy import Table, Column, ForeignKey\nfrom sqlalchemy.ext.declarative import declarative_base\nBase = declarative_base()\nuser_roles = Table('user_roles', Base.metadata,\n    Column('user_id', ForeignKey('users.id')),\n    Column('role_id', ForeignKey('roles.id'))\n)\nclass User(Base):\n    __tablename__ = 'users'\n    roles = relationship('Role', secondary=user_roles)",
      "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": "2024-08-09",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}