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

  • SEARCH
  • Home
  • 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 800363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:16:52+00:00 2026-05-14T23:16:52+00:00

Is it possible to have multi-level polymorphism in SQLAlchemy? Here’s an example: class Entity(Base):

  • 0

Is it possible to have multi-level polymorphism in SQLAlchemy? Here’s an example:

class Entity(Base):
    __tablename__ = 'entities'
    id = Column(Integer, primary_key=True)
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    entity_type = Column(Unicode(20), nullable=False)
    __mapper_args__ = {'polymorphic_on': entity_type}

class File(Entity):
    __tablename__ = 'files'
    id = Column(None, ForeignKey('entities.id'), primary_key=True)
    filepath = Column(Unicode(255), nullable=False)
    file_type = Column(Unicode(20), nullable=False)
    __mapper_args__ = {'polymorphic_identity': u'file', 'polymorphic_on': file_type)

class Image(File):
    __mapper_args__ = {'polymorphic_identity': u'image'}
    __tablename__ = 'images'
    id = Column(None, ForeignKey('files.id'), primary_key=True)
    width = Column(Integer)
    height = Column(Integer)

When I call Base.metadata.create_all(), SQLAlchemy raises the following error:

IntegrityError: (IntegrityError) entities.entity_type may not be NULL`.

This error goes away if I remove the Image model and the polymorphic_on key in File.

What gives?

  • 1 1 Answer
  • 0 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-14T23:16:53+00:00Added an answer on May 14, 2026 at 11:16 pm

    Yes. The problem with your code is that you’re making Image a type of file, when you must aim for the head of the tree, making Image a type of Entity.

    Example:

    from sqlalchemy import (Table, Column, Integer, String, create_engine,
        MetaData, ForeignKey)
    from sqlalchemy.orm import mapper, create_session
    from sqlalchemy.ext.declarative import declarative_base
    
    e = create_engine('sqlite:////tmp/foo.db', echo=True)
    Base = declarative_base(bind=e)
    
    class Employee(Base):
        __tablename__ = 'employees'
    
        employee_id = Column(Integer, primary_key=True)
        name = Column(String(50))
        type = Column(String(30), nullable=False)
    
        __mapper_args__ = {'polymorphic_on': type}
    
        def __init__(self, name):
            self.name = name
    
    class Manager(Employee):
        __tablename__ = 'managers'
        __mapper_args__ = {'polymorphic_identity': 'manager'}
    
        employee_id = Column(Integer, ForeignKey('employees.employee_id'),
            primary_key=True)
        manager_data = Column(String(50))
    
        def __init__(self, name, manager_data):
            super(Manager, self).__init__(name)
            self.manager_data = manager_data
    
    class Owner(Manager):
        __tablename__ = 'owners'
        __mapper_args__ = {'polymorphic_identity': 'owner'}
    
        employee_id = Column(Integer, ForeignKey('managers.employee_id'),
            primary_key=True)
        owner_secret = Column(String(50))
    
        def __init__(self, name, manager_data, owner_secret):
            super(Owner, self).__init__(name, manager_data)
            self.owner_secret = owner_secret
    
    Base.metadata.drop_all()
    Base.metadata.create_all()
    
    s = create_session(bind=e, autoflush=True, autocommit=False)    
    o = Owner('nosklo', 'mgr001', 'ownerpwd')
    s.add(o)
    s.commit()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to have a multi level dropdown menu by using the elements
How can I have multi-column text flow in div for XHTML please? (if possible)
Is it possible to have a multi-line textbox (im using asp.net c#) where the
Possible Duplicate: Is stl vector concurrent read thread-safe? I have a multi-threaded program that
Possible Duplicate: php multi-dimensional array remove duplicate I have an array like this: $a
Are multi-threaded CLR stored procs possible? I have a data-intensive task with lots of
Is it possible have two projects with the same name in flex builder? Here
Is it possible to have a multi-line title in an Android alert dialog? I
Is it possible to have an App (running iOS4 on hardware supporting multi-tasking) which
is it possible to have a collation on a table? I build a multi

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.