Adonis.js-播种用户和个人资料引发数据库错误(Postgres)

问题描述 投票:1回答:1

我正在尝试为用户和配置文件建立一对一关系的种子文件,并且当前出现“错误:关系'user_profiles'不存在”。从挖掘的角度来看,在多对多关系的情况下,Adonis似乎将其视为枢纽表。我(想或打算)拥有的是用户和个人资料之间的一对一关系。提前致谢! SQL和Adonis的新手。附带说明,用户坚持使用db,但没有相应的配置文件。

// My User Schema

class UserSchema extends Schema {
  up () {
    this.create('users', (table) => {
      table.increments('id')
      table.string('username', 80).notNullable().unique()
      table.string('email', 254).notNullable().unique()
      table.string('password', 60).notNullable()
      // table.integer('profile_id').unsigned().references('id').inTable('userprofiles')
      table.timestamps()
    })
  }

  down () {
    this.drop('users')
  }
}
// My Profile Schema

class UserprofileSchema extends Schema {
  up () {
    this.create('userprofiles', (table) => {
      table.increments()
      table.string('first_name')
      table.string('last_name')
      // table.integer('user_id')
      //   .unsigned().references('id').inTable('users')
      table.integer('user_id')
        .unsigned()
        .index('user_id')

      table.foreign('user_id')
        .references('users.id')

      table.timestamps()
    })
  }

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

我的用户模型包括以下关系定义:

profile () {
  return this.hasOne('App/Models/UserProfile')
}
// Seed script

class UserSeeder {
  async run () {
    try {
      const user = await Factory.model('App/Models/User').create()
      const userProfile = await Factory.model('App/Models/UserProfile').make()
      userProfile.user_id = user.id
      await user.profile().save(userProfile)
    } catch (e) {
      console.log('Error From Seeder: ', e);
    }
  }
}

错误代码'42P01',如果需要,可以发布整个正文。谢谢!

adonis.js
1个回答
2
投票

在模型userProfile上,如下设置表名。

class User extends Model {
  static get table () {
    return 'userprofiles'
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.