tachybase_todo/docs/en-US/development/server/collections/association-fields.md

154 lines
3.8 KiB
Markdown
Raw Normal View History

2022-11-07 09:39:28 +08:00
# Association Fields
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.
```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.
```ts
{
name: 'posts',
fields: [
{
type: 'belongsTo',
name: 'user',
target: 'users',
foreignKey: 'userId',
},
],
}
```
2022-11-07 09:39:28 +08:00
## Relationship parameters
### BelongsTo
```ts
interface BelongsTo {
type: 'belongsTo';
name: string;
2022-11-07 09:39:28 +08:00
// defaults to name's plural
target?: string;
2022-11-07 09:39:28 +08:00
// The default value is the primary key of the target model, usually 'id'
targetKey?: any;
2022-11-07 09:39:28 +08:00
// defaults to target + 'Id'
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-07 09:39:28 +08:00
name: 'books',
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
}
],
}
```
### HasOne
```ts
interface HasOne {
type: 'hasOne';
name: string;
2022-11-07 09:39:28 +08:00
// defaults to name's plural
target?: string;
2022-11-07 09:39:28 +08:00
// The default value is the primary key of the source model, usually 'id'
sourceKey?: string;
2022-11-07 09:39:28 +08:00
// default value is the singular form of source collection name + 'Id'
foreignKey?: string;
2022-11-07 09:39:28 +08:00
foreignKey?}
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-07 09:39:28 +08:00
name: 'users',
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
}
],
}
```
### HasMany
```ts
interface HasMany {
type: 'hasMany';
name: string;
2022-11-07 09:39:28 +08:00
// defaults to name
target?: string;
2022-11-07 09:39:28 +08:00
// The default value is the primary key of the source model, usually 'id'
sourceKey?: string;
2022-11-07 09:39:28 +08:00
// the default value is the singular form of the source collection name + 'Id'
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-07 09:39:28 +08:00
name: 'posts',
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
}
],
}
```
### BelongsToMany
```ts
interface BelongsToMany {
type: 'belongsToMany';
name: string;
2022-11-07 09:39:28 +08:00
// default value is name
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
through?: string;
2022-11-07 09:39:28 +08:00
// defaults to the singular form of source collection name + 'Id'
foreignKey?: string;
2022-11-07 09:39:28 +08:00
// The default value is the primary key of the source model, usually id
sourceKey?: string;
2022-11-07 09:39:28 +08:00
// the default value is the singular form of target + 'Id'
otherKey?: string;
2022-11-07 09:39:28 +08:00
// the default value is the primary key of the target model, usually id
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
{
name: 'posts',
fields: [
{
2022-11-07 09:39:28 +08:00
type: 'believesToMany',
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
}
],
}
```