mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-02-01 12:27:50 +01:00
Add pivot table
This commit is contained in:
@@ -1,10 +1,14 @@
|
|||||||
import { DateTime } from 'luxon'
|
import { DateTime } from 'luxon'
|
||||||
import {BaseModel, column} from '@ioc:Adonis/Lucid/Orm'
|
import {BaseModel, column, manyToMany, ManyToMany} from '@ioc:Adonis/Lucid/Orm'
|
||||||
|
import Tag from "App/Models/Tag";
|
||||||
|
|
||||||
export default class Post extends BaseModel {
|
export default class Post extends BaseModel {
|
||||||
@column({ isPrimary: true })
|
@column({ isPrimary: true })
|
||||||
public id: number
|
public id: number
|
||||||
|
|
||||||
|
@manyToMany(() => Tag)
|
||||||
|
public tags: ManyToMany<typeof Tag>
|
||||||
|
|
||||||
@column()
|
@column()
|
||||||
public slug: string
|
public slug: string
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import {DateTime} from 'luxon'
|
import {DateTime} from 'luxon'
|
||||||
import {BaseModel, BelongsTo, belongsTo, column} from '@ioc:Adonis/Lucid/Orm'
|
import {BaseModel, BelongsTo, belongsTo, column, ManyToMany, manyToMany} from '@ioc:Adonis/Lucid/Orm'
|
||||||
import File from "App/Models/File";
|
import File from "App/Models/File";
|
||||||
|
import Tag from "App/Models/Tag";
|
||||||
|
|
||||||
export default class Project extends BaseModel {
|
export default class Project extends BaseModel {
|
||||||
@column({ isPrimary: true })
|
@column({ isPrimary: true })
|
||||||
@@ -24,6 +25,9 @@ export default class Project extends BaseModel {
|
|||||||
@column()
|
@column()
|
||||||
public fileId: number
|
public fileId: number
|
||||||
|
|
||||||
|
@manyToMany(() => Tag)
|
||||||
|
public tags: ManyToMany<typeof Tag>
|
||||||
|
|
||||||
@column.dateTime({ autoCreate: true })
|
@column.dateTime({ autoCreate: true })
|
||||||
public createdAt: DateTime
|
public createdAt: DateTime
|
||||||
|
|
||||||
|
|||||||
27
database/migrations/1629148789980_post_tag.ts
Normal file
27
database/migrations/1629148789980_post_tag.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import BaseSchema from '@ioc:Adonis/Lucid/Schema'
|
||||||
|
|
||||||
|
export default class PostTags extends BaseSchema {
|
||||||
|
protected tableName = 'post_tag'
|
||||||
|
|
||||||
|
public async up () {
|
||||||
|
this.schema.createTable(this.tableName, (table) => {
|
||||||
|
table.increments('id').primary()
|
||||||
|
table
|
||||||
|
.integer('post_id')
|
||||||
|
.unsigned()
|
||||||
|
.references('posts.id')
|
||||||
|
.onDelete('CASCADE')
|
||||||
|
table
|
||||||
|
.integer('tag_id')
|
||||||
|
.unsigned()
|
||||||
|
.references('tags.id')
|
||||||
|
.onDelete('CASCADE')
|
||||||
|
table.unique(['post_id', 'tag_id'])
|
||||||
|
table.timestamps(true, true)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public async down () {
|
||||||
|
this.schema.dropTable(this.tableName)
|
||||||
|
}
|
||||||
|
}
|
||||||
27
database/migrations/1629149023037_project_tag.ts
Normal file
27
database/migrations/1629149023037_project_tag.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import BaseSchema from '@ioc:Adonis/Lucid/Schema'
|
||||||
|
|
||||||
|
export default class ProjectTags extends BaseSchema {
|
||||||
|
protected tableName = 'project_tag'
|
||||||
|
|
||||||
|
public async up () {
|
||||||
|
this.schema.createTable(this.tableName, (table) => {
|
||||||
|
table.increments('id').primary()
|
||||||
|
table
|
||||||
|
.integer('project_id')
|
||||||
|
.unsigned()
|
||||||
|
.references('projects.id')
|
||||||
|
.onDelete('CASCADE')
|
||||||
|
table
|
||||||
|
.integer('tag_id')
|
||||||
|
.unsigned()
|
||||||
|
.references('tags.id')
|
||||||
|
.onDelete('CASCADE')
|
||||||
|
table.unique(['project_id', 'tag_id'])
|
||||||
|
table.timestamps(true, true)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public async down () {
|
||||||
|
this.schema.dropTable(this.tableName)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user