1

I want to have a base entity with a field deleted which marks a deleted record. And i have 2 subclasses, each of them to have their own table with all own columns:

from elixir import *
from sqlalchemy import create_engine


class Catalog(Entity):
    using_options(inheritance='concrete')
    deleted = Boolean

class Contact(Catalog):
    using_options(inheritance='concrete')
    name = Field(String(60))

class Location(Catalog):
    using_options(inheritance='concrete')
    name = Field(String(100))

setup_all()

metadata.bind = create_engine('sqlite:///', echo=True)
metadata.create_all()

And the result:

CREATE TABLE __main___catalog (
        id INTEGER NOT NULL, 
        PRIMARY KEY (id)
)

CREATE TABLE __main___contact (
        id INTEGER NOT NULL, 
        name VARCHAR(60), 
        PRIMARY KEY (id)
)

CREATE TABLE __main___location (
        id INTEGER NOT NULL, 
        name VARCHAR(100), 
        PRIMARY KEY (id)
)

Questions:

  1. How to avoid creation of a table for the base entity? - solved: using_options(abstract = True)
  2. Why field deleted is not in the created tables? - this solved - i forgot to put it inside a Field
  3. I want to avoid typing in each subclass using_options(inheritance='concrete') but still have "concrete inheritance". Is there a way to make it default for all subclasses?
Ben
  • 51,770
  • 36
  • 127
  • 149
warvariuc
  • 57,116
  • 41
  • 173
  • 227

1 Answers1

0

This works:

class Catalog(Entity):

    deleted = Field(Boolean)
    using_options(abstract = True, inheritance = 'concrete')        


class Contact(Catalog):

    name = Field(String(60))


class Location(Catalog):

    name = Field(String(100))

and creates the following tables:

CREATE TABLE __main___contact (
        id INTEGER NOT NULL, 
        deleted BOOLEAN, 
        name VARCHAR(60), 
        PRIMARY KEY (id), 
        CHECK (deleted IN (0, 1))
)

CREATE TABLE __main___location (
        id INTEGER NOT NULL, 
        deleted BOOLEAN, 
        name VARCHAR(100), 
        PRIMARY KEY (id), 
        CHECK (deleted IN (0, 1))
)
warvariuc
  • 57,116
  • 41
  • 173
  • 227