Defining an abstract method in a SQLAlchemy base class

user1475412

From http://docs.sqlalchemy.org/en/improve_toc/orm/extensions/declarative/mixins.html#augmenting-the-base I see that you can define methods and attributes in the base class.

I'd like to make sure that all the child classes implement a particular method. However, in trying to define an abstract method like so:

import abc
from sqlalchemy.ext.declarative import declarative_base

class Base(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def implement_me(self):
        pass

Base = declarative_base(cls=Base)

class Child(Base):
    __tablename__ = 'child'

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

I tried to find some documentation or examples to help me but came up short.

mgilson

It looks like you can pass a metaclass to declarative_base. The default metaclass is DeclarativeMeta -- So, I think the key would be to create a new metaclass that is a mixin of abc.ABCMeta and DeclarativeMeta:

import abc
from sqlalchemy.ext.declarative import declarative_base, DeclarativeMeta

class DeclarativeABCMeta(DeclarativeMeta, abc.ABCMeta):
    pass

class Base(declarative_base(metaclass=DeclarativeABCMeta)):
    __abstract__ = True
    @abc.abstractmethod
    def implement_me(self):
        """Canhaz override?"""

*Untested

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Defining all abstract method in one concrete class

Method overloading with an abstract base class

Should I use interface or abstract base class defining List functionalities

Is there any solution to handle Transactional by defining in Abstract Class of Concrete Method?

Call through the base class abstract method

Defining an abstract class without any abstract methods

Can a default method implementation be used to implement an abstract method in a base class

Error "__init__ method from base class is not called" for an abstract class

Pass a List of a class into method with list of an abstract base class as a parameter

Abstract Base Class or Class?

Serialize objects with inheritance by defining serialize method only in base class?

Abstract class without defining in derived class

Overriding a method which has a parameter of the abstract base class

Can a trait implement an abstract method from a base class

Access public method defined on abstract base class from Clojure

C++ Abstract Base Template Class Non-Void Method

Call no-virtual subclass method from a pointer base abstract class

What does the Abstract Base Class register method actually do?

Defining a class with a method

defining method of class in if statement

When defining an abstract class, can you force a child to define a property/method defined in a parent interface?

Abstract method in a non abstract class

Abstract Method In Abstract Class In java

Assertion in abstract base class?

Abstract base bean class

Abstract base class in Dart

Abstract base class definition

Protected abstract or public abstract method in abstract class

PHP defining private abstract function/method

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive