2012-06-16 49 views
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')鍵入,但仍然有「具體繼承」。有沒有辦法讓所有的子類都成爲默認的?

回答

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