1

I'd like to use an abstract base class as declarative base class for my SQLAlchemy model.

Take the following ABC:

from abc import ABCMeta, abstractmethod


class DealMeta(ABCMeta):
    pass


class DealBase(metaclass=DealMeta):
    """
    """
    def __init__(self, valuation_model):
        self.valuation_model = valuation_model

    @abstractmethod
    def create(self):
        """
        """
        raise NotImplementedError()

    @property
    def valuation(self):
        return self.valuation_model.get_valuation(self)

And the following model:

from sqlalchemy import Column, Integer, Float, String
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base(cls=DealBase)


class Deal(Base):
    __tablename__ = "deals"

    # columns
    deal_name = Column(String)
    customer_name = Column(String)

    def __init__(self, **kwargs):
        # Call the constructur
        super(Deal, self).__init__(**kwargs)

    def create(self):
        pass

This returns the following error:

When importing Deal I receive the following error:

TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

I'm confused about the error. Which is the metaclass that must be subclassed here?

How do I do this properly?

Jason Strimpel
  • 14,670
  • 21
  • 76
  • 106

0 Answers0