1

我想要一个带有deleted标记已删除记录的字段的基本实体。我有 2 个子类,每个子类都有自己的表和所有自己的列:

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()

结果:

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)
)

问题:

  1. 如何避免为基础实体创建表?- 解决了:using_options(abstract = True)
  2. 为什么字段deleted不在创建的表中?- 这解决了 - 我忘了把它放在里面Field
  3. 我想避免输入每个子类using_options(inheritance='concrete'),但仍然有“具体继承”。有没有办法让它默认所有子类?
4

1 回答 1

0

这有效:

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))

并创建以下表格:

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))
)
于 2012-06-17T11:09:24.407 回答