Выходит ошибка при добавлении данных
sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'Mapper[Devices(devices)]'. Original exception was: When initializing mapper Map
per[Devices(devices)], expression 'drivers' failed to locate a name ('drivers'). If this is a class name, consider adding this relationship() to the <class 'src.devices.model.Devices'> class after both dependent classes have been
defined.
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import relationship, clear_mappers, configure_mappers, declarative_base
from core.database import Base
# Base = declarative_base()
class TypeDevice(Base):
__tablename__ = 'type_device'
id = Column(Integer, primary_key=True, nullable=False)
name = Column(String(200), nullable=False, unique=True)
# device = relationship("devices", back_populates="type_device", uselist=False)
class Config:
orm_mode = True
from sqlalchemy import Column, ForeignKey, Integer, String, Table
from sqlalchemy.orm import relationship, declarative_base
from core.database import Base
# Base = declarative_base()
drivers_devices = Table(
"drivers_devices",
Base.metadata,
Column("device_id", ForeignKey("devices.id")),
Column("driver_id", ForeignKey("drivers.id")),
)
class Driver(Base):
__tablename__ = 'drivers'
id = Column(Integer, primary_key=True, nullable=False)
name = Column(String(200), nullable=False)
position_save = Column(String(200))
model_device = Column(String(200))
devices = relationship('Devices', backref='driver')
class Config:
orm_mode = True
# class DriversDevices(Base):
# __tablename__ = 'drivers_devices'
# id = Column(Integer, primary_key=True, nullable=False)
# device_id = Column(Integer, ForeignKey('devices.id'))
# driver_id = Column(Integer, ForeignKey('drivers.id'))
#
from sqlalchemy import Column, Integer, String, Boolean, ForeignKey
from sqlalchemy.orm import relationship, declarative_base
from core.database import Base
# Base = declarative_base()
class Devices(Base):
__tablename__ = 'devices'
id = Column(Integer, primary_key=True, nullable=False)
name = Column(String(200), nullable=False)
hide = Column(Boolean)
drivers = relationship('drivers')
rmk_id = Column(Integer, ForeignKey('rmk.id'))
type_device_id = Column(Integer, ForeignKey('type_device.id'))
class Config:
orm_mode = True
Ошибка выходит когда я добавляю данные в type_device
В своих моделях я использую from core.database import Base.
from src.devices.model import Devices
from src.drivers.model import Driver
from src.devices.model import Devices
from src.type_device.model import TypeDevice
from src.quantity_products.model import QuantityProducts
from src.rmk.model import Rmk
from src.store.model import Store
from .database import Base
Но когда я пишу в каждой модели Base = declarative_base()
То данные добавляются. В чем моя ошибка, правильно ли я делаю если экспортирую Base из from core.database import Base.
как это можно исправить