首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在belongsToMany中建立adonis.js关系

如何在belongsToMany中建立adonis.js关系
EN

Stack Overflow用户
提问于 2020-01-07 22:39:42
回答 1查看 1.6K关注 0票数 1

我试着在两张桌子之间建立一种关系。我的关系是belongsToMany之间的用户=> user_bet_match =>匹配。用户可以拥有多个user_bet_match,匹配可以有多个user_bet_match。

我的数据库迁移是: Matchs表:

代码语言:javascript
复制
    this.create('matchs', (table) => {
      table.increments()
      table.integer('round_id').unsigned()
      table.integer('league_id').unsigned()
      table.integer('hometeam_id').unsigned()
      table.integer('awayteam_id').unsigned()
      table.string('final_score_hometeam_goal')
      table.string('final_score_awayteam_goal')
      table.string('halftime_score_hometeam_goal')
      table.string('halftime_score_awayteam_goal')
      table.date('event_date')
      table.integer('event_timestamp')
      table.boolean('betailable').defaultTo(false)
      table.boolean('is_finish').defaultTo(false)
      table.timestamps()
    })

用户表:

代码语言:javascript
复制
    this.create('users', (table) => {
      table.increments()
      table.string('username', 80).notNullable().unique()
      table.string('email', 254).notNullable().unique()
      table.string('password', 60).notNullable()
      table.timestamps()
    })

user_bet_match表:

代码语言:javascript
复制
    this.create('user_bet_match', (table) => {
      table.increments()
      table.integer('user_id').unsigned()
      table.integer('match_id').unsigned()
      table.string('choice').notNullable()
      table.timestamps()
    })

我的用户模型:

代码语言:javascript
复制
class User extends Model {
  static boot () {
    super.boot()

    this.addHook('beforeSave', async (userInstance) => {
      if (userInstance.dirty.password) {
        userInstance.password = await Hash.make(userInstance.password)
      }
    })
  }

  tokens () {
    return this.hasMany('App/Models/Token')
  }
  match () {
    return this.belongsToMany('App/Models/Match').pivotTable('user_bet_match')
  }

我的用户打赌匹配模块:

代码语言:javascript
复制
'use strict'

/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
const Model = use('Model')
const Database = use('Database')



class UserBetMatch extends Model {

  user () {
    return this.hasOne('App/Models/User')
  }
  matchs () {
    return this.hasOne('App/Models/Match')
  }
}

module.exports = UserBetMatch

我的配对模块:

代码语言:javascript
复制
'use strict'

/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
const Model = use('Model')

class Match extends Model {
  userbetmatchs () {
    return this.hasMany('App/Models/UserBetMatch')
  }
}

module.exports = Match

当我做出:

代码语言:javascript
复制
let k = user.match().fetch()

通过这一关系:

代码语言:javascript
复制
  match () {
    return this.belongsToMany('App/Models/Match').pivotTable('user_bet_match')
  }

它还给我sqlMessage: "Table 'bo7jjjccwliucibms5pf.matches' doesn't exist",但我从来没提过一张桌子“火柴”。我不知道为什么。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-08 08:39:31

我注意到您更改了迁移中表的名称(默认情况下使用adonis : matches;user_bet_matches)

试着在你的模型中使用这个:

代码语言:javascript
复制
static get table () {
    return 'matchs' // Your table name
}

^ https://adonisjs.com/docs/4.0/lucid#_table

清醒并没有考虑到迁移。因此,如果表不是默认的表(使用adonis cli),则必须指定该表的名称。

如果不公平,请告诉我。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59637090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档