我正在为我的定制吉他公司建立网站,我只是意识到数据库需要某种工作方式,我似乎无法全神贯注-也许我想得太多了,迷惑自己。
我需要相互关联两个表:一个Artists
表和一个Projects
表。 artists
表存储有关单个艺术家的信息,而Projects
表存储有关乐队/项目的信息。
创建Artists
表并将其主键用作Projects
表中的外键非常容易;如果一位艺术家参与了多个项目,那安排就没问题了。但是我想到的是,一个项目也可能与多于一位艺术家相关联。
我知道,如果artist_id
字段具有多个值(非原子),则在Projects
表中将*ngFor
字段作为外键与常规格式不一致;但我不确定我还能如何实现这一目标。
了解用例也可能会有所帮助:
我正在构建一个Django-REST后端,它将被Angular前端使用。有一个页面包含艺术家个人资料,Angular使用DOM
从JSON解析了艺术家个人资料。因此,用*ngFor
添加到DOM
的每个html块都将显示艺术家姓名,个人简介和图片;与艺术家相关的项目通过一个内部ngFor循环添加到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;
}
}
中。
以下是Angular的数据结构:
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>
这是显示数据的模板:
$total = 0;
$pizzas = array('kebabpizza' => 80, 'calzone' => 60, 'vesuvio' => 60);
foreach ($pizzas as $pizza => $price) {
echo $pizza . " - $price<br>";
}
$total +=$price;
echo "Your order is gonna cost you $total kr<hr>";
答案 0 :(得分:2)
我对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) |
+----------------------+ +-----------------+