我必须管理艺术家和专辑表。
| artists | | albums | | album_artist |
+--------------+ +--------------+ +--------------+
| id | | id | | id |
| artist | | album | | album_id |
| created_at | | created_at | | artist_id |
| updated_at | | updated_at | +--------------+
+--------------+ +--------------+
请记住,这是一个多对多的关系,我确实需要找到一种方法来使专辑艺术家对唯一,因为专辑可能具有相同的名称但属于不同的艺术家(例如2Pac的"Greatest Hits"专辑和Notorious BIG的"Greatest Hits")。是否有已知的方法/模式来解决此问题?
谢谢。
我以前做过,在album_id,artist_id上创建了一个唯一的约束。
这将使为不同的艺术家制作专辑成为可能。正如艺术家 id 会有所不同。在你的迁移放。
$table->unique(['album_id', 'artist_id']);
在传统的数据库设计中,这称为 composite key
,它是两列或多列,这使得表唯一,在大多数情况下pivot table
这将是两个foreign keys
。