[英]SQL Relationship with Multiple Foreign Keys
我正在为我的定制吉他公司建立网站,但我刚刚意识到数据库需要某种工作方式,这似乎并不能使我全神贯注-也许是我想得太多,并使自己感到困惑。
我需要将两个表相互关联:一个Artists
表和一个Projects
表。 artists
表存储有关单个艺术家的信息, Projects
表存储有关乐队/项目的信息。
创建Artists
表并将其主键用作Projects
表中的外键非常容易。 如果一位艺术家参与了多个项目,那安排就没问题了。 但是我想到的是,一个项目也可能与多于一位艺术家相关联。
我知道,如果artist_id
字段具有多个值(非原子性),则在Projects
表artist_id
字段作为外键与它artist_id
; 但我不确定我还能如何实现这一目标。
了解用例也可能会有所帮助:
我正在构建Django-REST后端,它将被Angular前端使用。 有一个包含艺术家个人资料的页面,Angular使用*ngFor
从Angular对其进行了解析。 因此,每个通过*ngFor
添加到DOM
的html块*ngFor
将显示艺术家姓名,个人简介和图片; 与艺术家相关的项目通过一个内部ngFor循环添加到DOM
。
以下是Angular的数据结构:
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;
}
}
这是您在上面看到的社交媒体模型,但这将是与Artists_Social
表的一对一正向关系:
export class ArtistSocialMediaModel {
public facebook: string;
public twitter: string;
public instagram: string;
constructor(facebook: string, twitter: string, instagram: string) {
this.facebook = facebook;
this.twitter = twitter;
this.instagram = instagram;
}
}
这是显示数据的模板:
<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>
我对Django一无所知,但是您要问的是多对多关系。 在大多数数据库系统中,多对多是通过第三个表实现的,该表具有要链接的表的外键。 某些数据库系统允许您将数组存储为行的成员,可用于此目的。 但是,这很罕见(通常仅在分层数据库中存在)。 第三表方法是最广泛应用的方法。
在您的情况下,您的表将如下所示。 请注意, Artist_Projects
表的主键是组合键-它是artist_id
和project_id
的组合 ,是主键。 但是这些字段中的每一个都是单独表的外键。
+----------------+ +----------------------+
| Artists | | Artist_Projects | +-----------------+
+----------------+ +----------------------+ | Projects |
| artist_id (PK) | <--- | artist_id (PK) (FK) | +-----------------+
+----------------+ | project_id (PK) (FK) | ---> | project_id (PK) |
+----------------------+ +-----------------+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.