如何使用Powerdesigner生成数据库表INHERITANCE postgresql脚本

时间:2013-07-16 05:20:16

标签: postgresql powerdesigner

我有两个表,“child”和“parent”,“child”必须继承“parent”的所有列,我在概念模型(类图)中使用两个表之间的“Generalization”链接,当我生成物理模型它给我这个结果:

create table child (
   id                   INT4                 null,
); 

create table parent (
   id                   INT4                 null,
   name                 INT4                 null
);

alter table child
   add constraint FK_CHILD_GENERALIZ_PARENT foreign key (id)
      references parent(id)
      on delete restrict on update restrict;

但是在Postgresql中,继承不是FK约束,它必须给我:

create table child (

); INHERITS (parent);

我想念的是什么?

1 个答案:

答案 0 :(得分:1)

从概念模型生成Postgresql物理模型时,似乎没有保留继承信息。

作为练习(对于 fun ,你可能会说),我设计了一个粗略的解决方案......

  1. 拥有Postgres DBMS的可编辑版本(使用标准版本,或创建副本,或将其嵌入物理模型中)
  2. 在此DBMS中,在Profile>下添加转换“PreserveInherits”。表(见下文)
  3. 在DBMS中,使用模型类型“概念数据模型”添加转换配置文件“PreserveInheritance”,使用上述“PreserveInherits”转换添加后生成步骤
  4. 使用工具>在CDM中生成物理数据模型,确保在PDM生成选项的“详细信息”选项卡中按下/选中“启用转换”
  5. 该技巧通过覆盖表格上的物理选项来生成所需的“继承(foo)”选项。
  6. PreserveInherits转换的来源:

    dim targetmap
    dim targetmapcreation
    
    sub DescentTargets(pkg, trfm)
       dim obj
       for each obj in pkg.tables
          if not (obj.IsShortcut) then
             dim src : set src = trfm.GetSource(obj)
             if not (src is nothing) then targetmap.add src,obj
           end if
       next
       for each obj in pkg.Packages
          DescentTargets obj,trfm
       next
    end sub
    
    function GetTargetObject(obj, model, trfm)
       ' global dims are not reset betweeen generations, use a timer to reset it sometimes
       if isempty(targetmap) or datediff("s",targetmapcreation,time)>60 then
          set targetmap = CreateObject("Scripting.Dictionary")
          targetmapcreation = time
          ' fill map with information about target objects
          DescentTargets model,trfm
       end if
       if targetmap.Exists(obj) then set GetTargetObject = targetmap.Item(obj)
    end function
    
    Sub %Transformation%(table, trfm)
       ' find origin entity
       dim source : set source = trfm.GetSource(table)
       if not (source is nothing) and source.IsShortcut then set source = source.TargetObject
       if not (source is nothing) and source.ClassKind = cls_Entity then
         ' walk up to parent entity
          dim link
          for each link in source.InheritsFrom
             dim cdmparent : set cdmparent = link.ParentEntity
             if not (cdmparent is nothing) and cdmparent.IsShortcut then cdmparent = cdmparent.TargetObject
             if not (cdmparent is nothing) then
                ' walk "back" to target table of parent entity
                ' trfm.GetParent(cdmparent) does not work as I hoped, use a helper function
                dim pdmparent : set pdmparent = GetTargetObject(cdmparent,table.model,trfm)
                if not (pdmparent is nothing) then
                   ' msgbox "found parent table " & pdmparent.name
                   table.physicaloptions = "inherits (" & pdmparent.code & ")"
                end if
             end if
             ' TODO modify the code to deal with several inheritances...
             exit for
          next
       end if
    End Sub