2022-11-07 09:39:28 +08:00
# Association Fields
2022-11-02 11:33:07 +08:00
2022-11-07 09:39:28 +08:00
In a relational database, the standard way to build a table relationship is to add a foreign key field followed by a foreign key constraint. For example, Knex builds a table with the following example.
2022-11-02 11:33:07 +08:00
```ts
knex.schema.table('posts', function (table) {
table.integer('userId').unsigned();
table.foreign('userId').references('users.id');
});
```
2022-11-07 09:39:28 +08:00
This procedure creates a userId field in the posts table and sets the foreign key constraint posts.userId to refer to users.id. In NocoBase's Collection, such a relational constraint is created by configuring the relational field, e.g.
2022-11-02 11:33:07 +08:00
```ts
{
name: 'posts',
fields: [
{
type: 'belongsTo',
name: 'user',
target: 'users',
foreignKey: 'userId',
},
],
}
```
2022-11-07 09:39:28 +08:00
## Relationship parameters
2022-11-02 11:33:07 +08:00
### BelongsTo
```ts
interface BelongsTo {
type: 'belongsTo';
name: string;
2022-11-07 09:39:28 +08:00
// defaults to name's plural
2022-11-02 11:33:07 +08:00
target?: string;
2022-11-07 09:39:28 +08:00
// The default value is the primary key of the target model, usually 'id'
2022-11-02 11:33:07 +08:00
targetKey?: any;
2022-11-07 09:39:28 +08:00
// defaults to target + 'Id'
2022-11-02 11:33:07 +08:00
foreignKey?: any;
}
2022-11-07 09:39:28 +08:00
// The authors table's primary key id is concatenated with the books table's foreign key authorId
2022-11-02 11:33:07 +08:00
{
2022-11-07 09:39:28 +08:00
name: 'books',
2022-11-02 11:33:07 +08:00
fields: [
{
type: 'belongsTo',
name: 'author',
target: 'authors',
2022-11-07 09:39:28 +08:00
targetKey: 'id', // authors table's primary key
foreignKey: 'authorId', // foreign key in books table
2022-11-02 11:33:07 +08:00
}
],
}
```
### HasOne
```ts
interface HasOne {
type: 'hasOne';
name: string;
2022-11-07 09:39:28 +08:00
// defaults to name's plural
2022-11-02 11:33:07 +08:00
target?: string;
2022-11-07 09:39:28 +08:00
// The default value is the primary key of the source model, usually 'id'
2022-11-02 11:33:07 +08:00
sourceKey?: string;
2022-11-07 09:39:28 +08:00
// default value is the singular form of source collection name + 'Id'
2022-11-02 11:33:07 +08:00
foreignKey?: string;
2022-11-07 09:39:28 +08:00
foreignKey?}
2022-11-02 11:33:07 +08:00
2022-11-07 09:39:28 +08:00
// The users table's primary key id is concatenated with the profiles' foreign key userId
2022-11-02 11:33:07 +08:00
{
2022-11-07 09:39:28 +08:00
name: 'users',
2022-11-02 11:33:07 +08:00
fields: [
{
type: 'hasOne',
name: 'profile',
target: 'profiles',
2022-11-07 09:39:28 +08:00
sourceKey: 'id', // users table's primary key
foreignKey: 'userId', // foreign key in profiles table
2022-11-02 11:33:07 +08:00
}
],
}
```
### HasMany
```ts
interface HasMany {
type: 'hasMany';
name: string;
2022-11-07 09:39:28 +08:00
// defaults to name
2022-11-02 11:33:07 +08:00
target?: string;
2022-11-07 09:39:28 +08:00
// The default value is the primary key of the source model, usually 'id'
2022-11-02 11:33:07 +08:00
sourceKey?: string;
2022-11-07 09:39:28 +08:00
// the default value is the singular form of the source collection name + 'Id'
2022-11-02 11:33:07 +08:00
foreignKey?: string;
}
2022-11-07 09:39:28 +08:00
// The posts table's primary key id is concatenated with the comments table's postId
2022-11-02 11:33:07 +08:00
{
2022-11-07 09:39:28 +08:00
name: 'posts',
2022-11-02 11:33:07 +08:00
fields: [
{
type: 'hasMany',
name: 'comments',
target: 'comments',
2022-11-07 09:39:28 +08:00
sourceKey: 'id', // posts table's primary key
foreignKey: 'postId', // foreign key in the comments table
2022-11-02 11:33:07 +08:00
}
],
}
```
### BelongsToMany
```ts
interface BelongsToMany {
type: 'belongsToMany';
name: string;
2022-11-07 09:39:28 +08:00
// default value is name
2022-11-02 11:33:07 +08:00
target?: string;
2022-11-07 09:39:28 +08:00
// defaults to the source collection name and target in the natural order of the first letter of the string
2022-11-02 11:33:07 +08:00
through?: string;
2022-11-07 09:39:28 +08:00
// defaults to the singular form of source collection name + 'Id'
2022-11-02 11:33:07 +08:00
foreignKey?: string;
2022-11-07 09:39:28 +08:00
// The default value is the primary key of the source model, usually id
2022-11-02 11:33:07 +08:00
sourceKey?: string;
2022-11-07 09:39:28 +08:00
// the default value is the singular form of target + 'Id'
2022-11-02 11:33:07 +08:00
otherKey?: string;
2022-11-07 09:39:28 +08:00
// the default value is the primary key of the target model, usually id
2022-11-02 11:33:07 +08:00
targetKey?: string;
}
2022-11-07 09:39:28 +08:00
// tags table's primary key, posts table's primary key and posts_tags two foreign keys are linked
2022-11-02 11:33:07 +08:00
{
name: 'posts',
fields: [
{
2022-11-07 09:39:28 +08:00
type: 'believesToMany',
2022-11-02 11:33:07 +08:00
name: 'tags',
target: 'tags',
2022-11-07 09:39:28 +08:00
through: 'posts_tags', // intermediate table
foreignKey: 'tagId', // foreign key 1, in posts_tags table
otherKey: 'postId', // foreignKey2, in posts_tags table
targetKey: 'id', // tags table's primary key
sourceKey: 'id', // posts table's primary key
2022-11-02 11:33:07 +08:00
}
],
}
```