Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有多个外键的SQL关系_Sql_Database_Postgresql_Django Rest Framework - Fatal编程技术网

具有多个外键的SQL关系

具有多个外键的SQL关系,sql,database,postgresql,django-rest-framework,Sql,Database,Postgresql,Django Rest Framework,我正在为我的定制吉他公司建立一个网站,我刚刚意识到数据库的工作方式有一些我无法理解的地方——或者我想得太多了,弄糊涂了 有两个表需要相互关联:一个Artists表和一个Projects表。artists表存储单个艺术家的信息,Projects表存储乐队/项目的信息 创建艺术家表并将其主键用作项目表中的外键非常简单;如果一位艺术家参与了多个项目,那么这种安排就没有问题。然而,我想到的是,一个项目也完全有可能与多个艺术家关联 我知道,如果项目表中有多个值(非原子值),则在项目表中将艺术家id字段作为

我正在为我的定制吉他公司建立一个网站,我刚刚意识到数据库的工作方式有一些我无法理解的地方——或者我想得太多了,弄糊涂了

有两个表需要相互关联:一个
Artists
表和一个
Projects
表。
artists
表存储单个艺术家的信息,
Projects
表存储乐队/项目的信息

创建
艺术家
表并将其主键用作
项目
表中的外键非常简单;如果一位艺术家参与了多个项目,那么这种安排就没有问题。然而,我想到的是,一个项目也完全有可能与多个艺术家关联

我知道,如果
项目
表中有多个值(非原子值),则在
项目
表中将
艺术家id
字段作为外键与正常形式不一致;但我不确定我还能如何做到这一点

了解用例也会有所帮助:

我正在构建Django REST后端,它将被一个有角度的前端占用。有一个包含艺术家简介的页面,Angular使用
*ngFor
解析JSON。因此,使用
*ngFor
添加到
DOM
的每个html块都将显示艺术家的姓名、简历和图片;艺术家关联的项目通过内部ngFor循环添加到
DOM

以下是角度方面的数据结构:

import {ArtistSocialMediaModel} from './artist-social-media.model';

export class ArtistProfilesModel {
  public artist_name: string;
  public artist_image: string;
  // the second string is a range of active dates for a given project
  // which I will convert to a string in Django before serializing
  public projects: Array<[string, string]>;
  public description: string;
  public band_website: string;
  public social_media: ArtistSocialMediaModel[];

  constructor(name: string, image: string, projects,
          description: string, website: string, social) {


    this.artist_name = name;
    this.artist_image = image;
    this.projects = projects;
    this.description = description;
    this.band_website = website;
    this.social_media = social;
  }

}
这是显示数据的模板:

    <div *ngFor="let profile of artistProfiles; let i = index"
     class="profiles-div">

    <div *ngIf="resolveIndex(i) === 'left'; then left else right">ignored</div>

    <ng-template #left>
      <div class="row">

      <div class="col-6 col-md-5">

        <img [src]="profile.artist_image"
             [alt]="profile.artist_name"
             class="img-thumbnail img-fluid"
             [ngStyle]="{ 'float': resolveIndex(i)}">

        <h1 class="artists-jumbo-header"
            [ngStyle]="{ 'text-align': resolveIndex(i)}">
          Projects:
        </h1>
        <p *ngFor="let project of profile.projects"
           [ngStyle]="{ 'text-align': resolveIndex(i)}"
           class="artists-p">
          {{project[0] + ": " + project[1]}}
        </p>

        <h1 class="artists-jumbo-header"
            [ngStyle]="{ 'text-align': resolveIndex(i)}">
          Website:
        </h1>
        <a href="https:{{profile.band_website}}"
           target="_blank">
          <p [ngStyle]="{ 'text-align': resolveIndex(i)}"
             class="artists-p">
            {{profile.band_website}}
          </p>
        </a>

        <h1 class="artists-jumbo-header"
            [ngStyle]="{ 'text-align': resolveIndex(i)}">
          Social Media:
        </h1>
        <a href="https://{{profile.social_media['facebook']}}"
           target="_blank">
          <p [ngStyle]="{ 'text-align': resolveIndex(i)}"
             class="artists-p">{{profile.social_media['facebook']}}</p>
        </a>

        <a href="https://{{profile.social_media['twitter']}}"
           target="_blank">
          <p [ngStyle]="{ 'text-align': resolveIndex(i)}"
             class="artists-p">{{profile.social_media['twitter']}}</p>
        </a>

        <a href="https://{{profile.social_media['instagram']}}"
           target="_blank">
          <p [ngStyle]="{ 'text-align': resolveIndex(i)}"
             class="artists-p">
            {{profile.social_media['instagram']}}</p>
        </a>
      </div>

      <div class="col-6 col-md-7">
        <h1 class="artists-jumbo-header">{{ profile.artist_name }}
        </h1>
        <br>
        <p class="artists-p">{{ profile.description }}</p>
      </div>
    </div>
   </ng-template>

   <ng-template #right>
    <div class="row ng-template-right">
      <div class="col-6 col-md-7">
        <h1 class="artists-jumbo-header">{{ profile.artist_name }}
        </h1>
        <br>
        <p class="artists-p">{{ profile.description }}</p>
      </div>
      <div class="col-6 col-md-5">
        <img [src]="profile.artist_image"
             [alt]="profile.artist_name"
             class="img-thumbnail"
             [ngStyle]="{ 'float': resolveIndex(i)}">

        <div class="container">
          <h1 class="artists-jumbo-header"
              [ngStyle]="{ 'text-align': resolveIndex(i)}">
            Projects:
          </h1>
          <p *ngFor="let project of profile.projects"
             [ngStyle]="{ 'text-align': resolveIndex(i)}"
             class="artists-p">
            {{project[0] + ": " + project[1]}}
          </p>

          <h1 class="artists-jumbo-header"
              [ngStyle]="{ 'text-align': resolveIndex(i)}">
            Website:
          </h1>
          <a href="https:{{profile.band_website}}"
             target="_blank">
            <p [ngStyle]="{ 'text-align': resolveIndex(i)}"
               class="artists-p">
              {{profile.band_website}}
            </p>
          </a>

          <h1 class="artists-jumbo-header"
              [ngStyle]="{ 'text-align': resolveIndex(i)}">
            Social Media:
          </h1>
          <a href="https://{{profile.social_media['facebook']}}"
             target="_blank">
            <p [ngStyle]="{ 'text-align': resolveIndex(i)}"
               class="artists-p">{{profile.social_media['facebook']}}</p>
          </a>

          <a href="https://{{profile.social_media['twitter']}}"
             target="_blank">
            <p [ngStyle]="{ 'text-align': resolveIndex(i)}"
               class="artists-p">{{profile.social_media['twitter']}}</p>
          </a>

          <a href="https://{{profile.social_media['instagram']}}"
             target="_blank">
            <p [ngStyle]="{ 'text-align': resolveIndex(i)}"
               class="artists-p">
              {{profile.social_media['instagram']}}</p>
          </a>
        </div>

      </div>
    </div>
  </ng-template>

  <hr>

</div>

忽略
项目:

{{project[0]+':“+project[1]}

网站: 社会化媒体: {{profile.artist_name}

{{profile.description}

{{profile.artist_name}

{{profile.description}

项目: {{project[0]+':“+project[1]}

网站: 社会化媒体:

我对Django一无所知,但你要问的是一种多对多的关系。在大多数数据库系统中,多对多是通过第三个表实现的,第三个表带有链接表的外键。某些数据库系统允许您将数组存储为行的成员,可以用于此目的。然而,这是一件罕见的事情(通常只出现在分层数据库中)。第三种表格方法是最广泛适用的

在你的情况下,你的桌子看起来像这样。请注意,
Artist\u Projects
表的主键是一个复合键——主键是
Artist\u id
project\u id
的组合。但这些字段中的每一个都是单独表的外键

+----------------+      +----------------------+      
| Artists        |      | Artist_Projects      |      +-----------------+
+----------------+      +----------------------+      | Projects        |
| artist_id (PK) | <--- | artist_id  (PK) (FK) |      +-----------------+
+----------------+      | project_id (PK) (FK) | ---> | project_id (PK) |
                        +----------------------+      +-----------------+
+--------------------++-------------------------+
|艺术家| |艺术家|项目|+-----------------+
+----------------++-------------------------+|项目|
|艺术家id(PK)|项目id(PK)|
+----------------------+      +-----------------+

我对Django一无所知,但你要问的是一种多对多的关系。在大多数数据库系统中,多对多是通过第三个表实现的,第三个表带有链接表的外键。某些数据库系统允许您将数组存储为行的成员,可以用于此目的。然而,这是一件罕见的事情(通常只出现在分层数据库中)。第三种表格方法是最广泛适用的

在你的情况下,你的桌子看起来像这样。请注意,
Artist\u Projects
表的主键是一个复合键——主键是
Artist\u id
project\u id
的组合。但这些字段中的每一个都是单独表的外键

+----------------+      +----------------------+      
| Artists        |      | Artist_Projects      |      +-----------------+
+----------------+      +----------------------+      | Projects        |
| artist_id (PK) | <--- | artist_id  (PK) (FK) |      +-----------------+
+----------------+      | project_id (PK) (FK) | ---> | project_id (PK) |
                        +----------------------+      +-----------------+
+--------------------++-------------------------+
|艺术家| |艺术家|项目|+-----------------+
+----------------++-------------------------+|项目|
|艺术家id(PK)|项目id(PK)|
+----------------------+      +-----------------+

不必了解Django,我只是觉得在这些问题中提供大量细节是很好的。我认为这可能是一个多对多的解决方案,但我不记得具体如何实现它。我想我应该在深入之前伸出手来,获得一些智能输入。谢谢你的回答,我会尝试一下,我很高兴投票@Hildy这件事运气好吗。如果这能满足你的需要,请注明答案。不必了解Django,我只是觉得在这些问题中给出很多细节是很好的。我认为这可能是一个多对多的解决方案,但我不记得具体如何实现它。我想我应该在深入之前伸出手来,获得一些智能输入。谢谢你的回答,我会尝试一下,我很高兴投票@Hildy这件事运气好吗。如果这是针对您的问题,请标记答案