I'm having an issue with circular imports in SQLAlchemy.
I have two files foo.py and bar.py. foo.py defines a SQLAlchemy class Foo, and bar.py defines a class Bar.
Both Foo and Bar are each other's foreign keys, so I map them to each other with Mapped["..."]
to get type safety, however that means I need to import the actual classes aswell.
This is causing a circular import error.
What's the best way to handle this issue? What are some general best practices for dealing with circular imports in SQLAlchemy? Can't you have type safety in this case if you use a relationship bidirectionally?
# foo.py
from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.orm import relationship
from .bar import Bar
class Foo(Base):
__tablename__ = 'foo'
id = Column(Integer, primary_key=True)
bar_id = Column(Integer, ForeignKey('bar.id'))
bar: Mapped["Bar"] = relationship('Bar')
# bar.py
from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.orm import relationship
from .foo import Foo
class Bar(Base):
__tablename__ = 'bar'
id = Column(Integer, primary_key=True)
foo_id = Column(Integer, ForeignKey('foo.id'))
foo: Mapped["Foo"] = relationship('Foo')
Edit:
Note that I can't remove the Bar and Foo imports because then Mapped["..."]
will raise an undefined error for "..."