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

- **ID:** `python/sqlalchemy-many-to-many-relationship-config`
- **Domain:** python
- **Category:** config_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

The association table used in a many-to-many relationship is defined with a different MetaData instance or not properly shared.

## Version Compatibility

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

## Workarounds

1. **** (95% success)
   ```
   from sqlalchemy import Table, Column, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
user_roles = Table('user_roles', Base.metadata,
    Column('user_id', ForeignKey('users.id')),
    Column('role_id', ForeignKey('roles.id'))
)
class User(Base):
    __tablename__ = 'users'
    roles = relationship('Role', secondary=user_roles)
   ```

## Dead Ends

- **** — Causes table redefinition and still may not share MetaData. (60% fail)
- **** — SQLAlchemy requires consistent metadata for relationships. (50% fail)
