E_UNSAVED_MODEL_INSTANCE many to many

#1

Hello,

I’m trying to make a query many to many, but I’m getting the following error.

RuntimeException

E_UNSAVED_MODEL_INSTANCE: Cannot process relation, since User model is not persisted to database or relational value is undefined > More details: https://err.sh/adonisjs/errors/E_UNSAVED_MODEL_INSTANCE

Tables
Capturar

Models

'use strict'

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

/** @type {import('@adonisjs/framework/src/Hash')} */
const Hash = use('Hash')

class User extends Model {
  static get table() {
    return 'USUARIO'
  }

  static boot() {
    super.boot()

    /**
     * A hook to hash the user password before saving
     * it to the database.
     */
    this.addHook('beforeSave', async (userInstance) => {
      if (userInstance.dirty.password) {
        userInstance.password = await Hash.make(userInstance.password)
      }
    })
  }

  /**
   * A relationship on tokens is required for auth to
   * work. Since features like `refreshTokens` or
   * `rememberToken` will be saved inside the
   * tokens table.
   *
   * @method tokens
   *
   * @return {Object}
   */
  tokens() {
    return this.hasMany('App/Models/Token')
  }

  perfis() {
    return this.belongsToMany('App/Models/Perfil').pivotModel(
      'App/Models/PerfilUsuario'
    )
  }

}

module.exports = User
'use strict'

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

class Perfil extends Model {
  static get table() {
    return 'PERFIL'
  }

  users() {
    return this.belongsToMany('App/Models/User').pivotModel(
      'App/Models/PerfilUsuario'
    )
  }

}

module.exports = Perfil

'use strict'

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

class PerfilUsuario extends Model {
  static get table() {
    return 'PERFIL_USUARIO'
  }

  user() {
    return this.belongsTo('App/Models/User')
  }

  perfil() {
    return this.belongsTo('App/Models/Perfil')
  }
}

module.exports = PerfilUsuario

Contoller

'use strict'
const User = use('App/Models/User')

class UserController {

  async index({ request }) {
    let user = await User.findByOrFail('USER_NAME', 'kdsilveira')

    user = await user.perfis().fetch()

    return user
  }

}

module.exports = UserController
0 Likes

#2

I am trying to understand what are you trying to achieve.

Can you share migration with us as well please?

Also Many profiles can be pointing to one user?

0 Likes

#3

I am accessing a database that was created without adonis. No migration was created.

Also Many profiles can be pointing to one user?
Yes, many profiles can point to a user.

0 Likes

#4

Well that may be causing the issue.

Looking at the error it says:

Why This Error Occurred
This error occurs when you are trying to call some method on the relationship, but related model has never been persisted to the database.

App thinks the database tables related to the model doesn’t exists, thus giving you that error.

I am unsure how would go around and fix that.

If I you can provide sql create queries I can try and replicate the issue…

1 Like

#5

– DDL for Table USUARIO

CREATE TABLE “USUARIO”
( “USER_NAME” VARCHAR2(50 BYTE),
“NOME” VARCHAR2(50 BYTE)
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE “DATPRICING” ;

– DDL for Table PERFIL_USUARIO

CREATE TABLE “PERFIL_USUARIO”
( “ID” NUMBER,
“PERFIL” NUMBER,
“USUARIO” VARCHAR2(50 BYTE)
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE “DATPRICING” ;

– DDL for Table PERFIL

CREATE TABLE “PERFIL”
( “ID” NUMBER,
“DESCRICAO” VARCHAR2(50 BYTE),
“ATIVO” CHAR(1 BYTE)
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE “DATPRICING” ;
REM INSERTING into USUARIO
SET DEFINE OFF;

0 Likes