gpt4 book ai didi

node.js - adonis/node.js 中不存在关系

转载 作者:太空宇宙 更新时间:2023-11-03 23:13:17 24 4
gpt4 key购买 nike

我有两个表:bookbook_units,如下所示:

class BookSchema extends Schema {
up () {
this.create('books', (table) => {
table.increments()
table.string('code').notNullable().unique()
table.string('description')
table.string('authors')
table.boolean('status').defaultTo(false)
table.integer('user_id').references('id').inTable('users')
table.timestamps()
})
}
}


class BookUnitSchema extends Schema {
up () {
this.create('book_unit', (table) => {
table.increments()
table.integer('book_id').references('id').inTable('books').notNullable()
table.integer('unit').notNullable().unique()
table.integer('sequence').unique()
table.string('description')
table.integer('qt_question')
table.boolean('status').defaultTo(false)
table.integer('user_id').references('id').inTable('users')
table.timestamps()
})
}
}

Book模型中,我定义了与book_units的关系:

class Book extends Model {

book_units () {
return this.hasMany('App/Models/BookUnit')
}

}

图书单元模型中:

class BookUnit extends Model {

book () {
return this.belongsTo('App/Models/Book')
}

}

我正在尝试使用以下 json 在 book_unit 中使用 postman 进行插入:

{
"book_id": 1,
"unit": 1,
"sequence": 1,
"description": "UNIT_01_GRAMMAR",
"qt_question": 5,
"status": false
}

但我收到:

insert into "book_units" ("book_id", "created_at", "description", "qt_question", "sequence", "status", "unit", "updated_at", "user_id") values ($1, $2, $3, $4, $5, $6, $7, $8, $9) returning "id" - relation "book_units" does not exist

数据库中存在 id 为 1 的这本书。为什么我收到此错误?

最佳答案

我测试了你的代码。

我注意到这个问题:

默认情况下,AdonisJS CLI 创建一个名为:book_units(您的代码:book_unit)的表,命令:> adonis make:migration BookUnit

我用(App/Models/BookUnit)解决了这个问题:

class BookUnit extends Model {
static get table() {
return 'book_unit'
} // Model table name

...

改变

this.create('book_unit', (table) => {

this.create('book_units', (table) => {

说明

模型和迁移没有关联。如果您在迁移中更改表的名称,则需要将其传递给模型。

完整代码

架构:

class BookUnitSchema extends Schema {
up() {
this.create('book_unit', (table) => {
table.increments()
table.integer('book_id').references('id').inTable('books').notNullable()
table.integer('unit').notNullable().unique()
table.integer('sequence').unique()
table.string('description')
table.integer('qt_question')
table.boolean('status').defaultTo(false)
table.integer('user_id').references('id').inTable('users')
table.timestamps()
})
}

down() {
this.drop('book_units')
}
}

//=====================================

class BookSchema extends Schema {
up() {
this.create('books', (table) => {
table.increments()
table.string('code').notNullable().unique()
table.string('description')
table.string('authors')
table.boolean('status').defaultTo(false)
table.integer('user_id').references('id').inTable('users')
table.timestamps()
})
}

down() {
this.drop('books')
}
}

型号:

class BookUnit extends Model {
static get table() {
return 'book_unit'
}

book() {
return this.belongsTo('App/Models/Book')
}
}

//==============================

class Book extends Model {
book_units() {
return this.hasMany('App/Models/BookUnit')
}
}

测试:

var data = {
"book_id": 1,
"unit": 1,
"sequence": 1,
"description": "UNIT_01_GRAMMAR",
"qt_question": 5,
"status": false
}
const bookUnit = await BookUnit.create(data)

结果: enter image description here

测试配置:

  • SQLite

如果您需要更多信息,请不要犹豫。

关于node.js - adonis/node.js 中不存在关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58665005/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com