# 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'

- **ID:** `python/sqlalchemy-circular-import-relationship`
- **Domain:** python
- **Category:** module_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Circular imports between modules when defining relationships, causing the mapper to fail to initialize.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.x | active | — | — |

## Workarounds

1. **Use string references in relationships** (98% success)
   ```
   class Parent(Base):
    __tablename__ = 'parents'
    children = relationship('Child', back_populates='parent')
class Child(Base):
    __tablename__ = 'children'
    parent_id = Column(Integer, ForeignKey('parents.id'))
    parent = relationship('Parent', back_populates='children')
   ```
2. **Use lazy imports inside the relationship definition** (90% success)
   ```
   children = relationship(lambda: Child, back_populates='parent')
   ```

## Dead Ends

- **Moving all models into one file** — This may resolve the import issue but leads to a monolithic, hard-to-maintain codebase. (50% fail)
- **Removing the relationship** — The ORM functionality is lost. (90% fail)
