Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3594466
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:43:03+00:00 2026-05-18T19:43:03+00:00

I have a declarative base class Entity which defines the column name as polymorphic,

  • 0

I have a declarative base class Entity which defines the column name as polymorphic, e.g.

class Entity(DeclarativeBase):
    name = Column('name', String(40))
    __mapper_args__ = {'polymorphic_on':name}

In subclasses, I could now say

class Experiment(Entity):
    __mapper_args__ = {'polymorphic_identity': "experiment"}

and be done. However, I would like to ease the creation of subclasses for the users of my library and therefore make the following possible:

  • Have a better syntax for it which is not as convoluted as the default syntax. This could be a simple assignment poly_id = "exp" inside the class or maybe a class decorator.
  • If no polymorphic_identity is given, extract the name from the name of the subclass.

I tried to get this done using metaclasses (only the second part):

from sqlalchemy.ext.declarative import DeclarativeMeta

class Meta(type):
    def __init__(cls, classname, bases, dict_):
        dict_["__mapper_args__"] = {'polymorphic_identity': classname}
        return super(Meta, cls).__init__(classname, bases, dict_)

class CombinedMeta(Meta, DeclarativeMeta):
    pass

class Experiment(Entity):
    __metaclass__ = CombinedMeta

so that, in my opinion, my Meta should set the name before calling the DeclarativeMeta but it does not seem to work. So either the DeclarativeMeta which supposedly sets the polymorphic name never sees the change because I messed up the MRO or what I’m doing is plain wrong anyway. What would I need to change or is there even something like this in SQLAlchemy already?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T19:43:04+00:00Added an answer on May 18, 2026 at 7:43 pm

    Shame on me but the problem could easily be solved by not using the dict_ but by setting the attribute directly on cls.

    class Meta(DeclarativeMeta):
        def __init__(cls, *args, **kw):
            if getattr(cls, '_decl_class_registry', None) is None:
                return # they use this in the docs, so maybe its not a bad idea
            cls.__mapper_args__ = {'polymorphic_identity': cls.__name__}
            return super(Meta, cls).__init__(*args, **kw)
    
    class Experiment(Entity):
        __metaclass__ = Meta
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a declarative table defined like this: class Transaction(Base): __tablename__ = transactions id
I have a class that inherits a base class to which another class has
I have a declarative SQLAlchemy object with deferred columns, declared like this: class Review(Base):
I have a table with the following declarative definition: class Type(Base): __tablename__ = 'Type'
I am writing a turbogears2 application. I have a table like this: class Order(DeclarativeBase):
I have a class mapped with a table, in my case in a declarative
SqlAlchemy newbie question: Base = declarative_base() class A(Base): __tablename__ = 'as' id = Column(Integer,
why if we have pure virtual assignment operator in a base class, then we
I have model like this: class CreatedMixin(DeclarativeBase): __abstract__ = True @declared_attr def updated_by(cls): return
So say, for example I have two classes: Base = declarative_base() class Vendor(Base): __tablename__

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.