Почему alembic не создает миграцию в моделях sqlalchemy?

Есть три модели с некоторыми связями между собой.
Модели
class Clinic(Base):
    '''Таблица клиники'''
    id = Column(Integer, primary_key=True, index=True)
    title = Column(String, index=True, nullable=False, comment="Наименование")
    website = Column(String, comment="Веб-сайт")
    phone = Column(String(21), comment="Телефон")
    address = Column(String(500), comment="Адрес")
    logo = Column(String(200), comment="Логотип")
    start_time = Column(Time, comment="Начало рабочего дня")
    end_time = Column(Time, comment="Конец рабочего дня")
    rate_id = Column(Integer, ForeignKey("rate.id"))
    users = relationship("User", back_populates="clinic")
    rate = relationship("Rate", back_populates="clinics")


class Rate(Base):
    '''Таблица с тарифами'''
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(50), unique=True, nullable=False, comment="Наименование")
    price = Column(Integer, nullable=False, comment="Стомость")
    is_active = Column(Boolean, default=True, comment="Активный")
    clinics = relationship("Clinic", back_populates="rate")


class User(Base):
    '''Модель пользователя'''
    id = Column(Integer, primary_key=True, index=True)
    first_name = Column(String, index=True)
    last_name = Column(String, index=True)
    patronymic = Column(String, index=True)
    birthday = Column(Date)
    image = Column(String, nullable=True)
    email = Column(String, unique=True, index=True, nullable=False)
    hashed_password = Column(String, nullable=False)
    is_active = Column(Boolean(), default=True)
    is_superuser = Column(Boolean(), default=False)
    clinic_id = Column(Integer, ForeignKey('clinic.id'))
    clinic = relationship("Clinic", back_populates="users")


При попытке создать миграцию алембик ругается.
sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'clinic.rate_id' could not find table 'rate' with which to generate a foreign key to target column 'id'
  • Вопрос задан
  • 562 просмотра
Решения вопроса 1
mgis
@mgis Автор вопроса
Помогло решение заменить
rate_id = Column(Integer, ForeignKey("rate.id"))
на
from app.models.rate import Rate #начала импортируем модель

rate_id = Column(Integer, ForeignKey(Rate.id))


Я так и не понял, почему вариант с передачей строки не сработал.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы