Я пытаюсь обернуть классы SQLAlchemy ORM несколькими методами.Все работает нормально, если я добавлю только один класс в свой класс.Но с двумя классами я сталкиваюсь с ошибкой
sqlalchemy.exc.InvalidRequestError: Class <class 'my_code.core.source.Source'> has multiple mapped bases: [<class 'my_code.core.source.SourceBase'>, <class 'my_code.core.source.SourceFieldBase'>
В my_code/core/source.py
,
class SourceBase(Base):
__tablename__ = 'cd_source'
id = Column(Integer, primary_key=True)
conn_id = Column(Integer, ForeignKey('cd_connection.id'))
name = Column(String(100))
tablename = Column(String(100))
class SourceFieldBase(Base):
__tablename__ = 'cd_source_field'
id = Column(Integer, primary_key=True)
source_id = Column(Integer, ForeignKey('cd_source.id'))
field = Column(String(100))
type = Column(ENUM('integer', 'smallint', 'bigint', 'string', 'bool',
'array', 'document', 'float', 'numeric', 'datetime', 'date', name='field_type'))
length = Column(Integer)
association = Column(Integer, ForeignKey('cd_source_field.id'))
filter_id = Column(Integer, ForeignKey('cd_filter.id'))
op = Column(ENUM('ge', 'gt', 'lt', 'le', 'eq', 'ne',
'not', 'is', name='operation_type'))
Теперь я создаю оболочку, которая использует оба класса.Метод add_source фактически добавляет данные в cd_source
, а также cd_source_field
таблицы.Я не уверен, что делать, поскольку документация по SQLAlchemy привела меня в замешательство.
class Source(SourceBase, SourceFieldBase):
def __init__(self):
self.engine = create_engine(SQLALCHEMY_CONN)
self.Session = sessionmaker(bind=self.engine)
def add_source(self, name, conn_id, tablename, field_list):
session = self.Session()
try:
source = SourceBase(name=name, conn_id=conn_id, tablename=tablename)
session.add(source)
source_id = source.id
for field in field_list:
source_field = SourceFieldBase(source_id=source_id, **field)
session.add(source_field)
session.commit()
return source.id
except Exception as e:
# TODO: Handle the exception by raising a custom Exception
traceback.print_exc()
finally:
if session.dirty:
session.commit()
Я новичок в sqlalchemy ORM, и я пытаюсь сделать это как экспериментальный проект, чтобы лучше его изучить.Пожалуйста, дайте мне знать, если это вопрос новичка.Я изучил документацию, но не могу понять, к какому разделу обращаться по этому поводу.
некоторые ссылки, которые я нашел, когда гуглил, но не понял ни слова об этом: https://github.com/sqlalchemy/sqlalchemy/issues/4699 https://github.com/sqlalchemy/sqlalchemy/issues/4321