我试着在两张桌子之间建立一种关系。我的关系是belongsToMany之间的用户=> user_bet_match =>匹配。用户可以拥有多个user_bet_match,匹配可以有多个user_bet_match。
我的数据库迁移是: Matchs表:
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()
})用户表:
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表:
this.create('user_bet_match', (table) => {
table.increments()
table.integer('user_id').unsigned()
table.integer('match_id').unsigned()
table.string('choice').notNullable()
table.timestamps()
})我的用户模型:
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')
}我的用户打赌匹配模块:
'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我的配对模块:
'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当我做出:
let k = user.match().fetch()通过这一关系:
match () {
return this.belongsToMany('App/Models/Match').pivotTable('user_bet_match')
}它还给我sqlMessage: "Table 'bo7jjjccwliucibms5pf.matches' doesn't exist",但我从来没提过一张桌子“火柴”。我不知道为什么。
发布于 2020-01-08 08:39:31
我注意到您更改了迁移中表的名称(默认情况下使用adonis : matches;user_bet_matches)
试着在你的模型中使用这个:
static get table () {
return 'matchs' // Your table name
}^ https://adonisjs.com/docs/4.0/lucid#_table
清醒并没有考虑到迁移。因此,如果表不是默认的表(使用adonis cli),则必须指定该表的名称。
如果不公平,请告诉我。
https://stackoverflow.com/questions/59637090
复制相似问题