代码之家  ›  专栏  ›  技术社区  ›  atkayla

具有多个配置文件的用户的PostgreSQL模型

  •  0
  • atkayla  · 技术社区  · 8 年前

    用户可以有3种类型的配置文件:A、B、C

    应该如何建模?

    • 未对字段进行筛选,例如,未对其中的名称进行筛选=
    • 无需担心的角色/权限

    profile
    ---
    name_A name_B name_C photos_A photos_B photos_C description_A description_B description_C
    

    加入另一个表:

    profile
    ---
    A_profile_id B_profile_id C_profile_id
    
    A_profile
    ---
    id name photos description
    
    B_profile
    ---
    id name photos description
    
    C_profile
    ---
    id name photos description
    

    jsonb(按字段):

    profile
    ---
    name{A,B,C} photos{A,B,C} description{A,B,C}
    

    profile
    ---
    A_profile{name,photos,description} B_profile{name,photos,description} C_profile{name,photos,description}
    

    嵌套的jsonb(诱人的…):

    profile
    ---
    profile{A{name,photos,description},B{name,photos,description},C{name,photos,description}}
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   nimish    8 年前

    其他每个选项都稍微灵活一些,但本质上只是一个非规范化版本。

        2
  •  0
  •   Gordon Linoff    8 年前

    create table Profiles (
        profileId serial,
        . . .
    );
    
    create table Persons (
       personId serial,
       . . .
       profile_A int references profiles(profileId),
       profile_B int references profiles(profileId),
       profile_C int references profiles(profileId)
    );
    

    尽管使用三个不同的列是有充分理由的,但您可能还需要一个连接表:

    create table personProfiles (
        personProfileId serial,
        personId int references person(personId),
        profileId int references profiles(profileId),
        profileTye <whatever>
    );