adonisjs / lucid

AdonisJS SQL ORM. Supports PostgreSQL, MySQL, MSSQL, Redshift, SQLite and many more
https://lucid.adonisjs.com/
MIT License
1.03k stars 189 forks source link

Preload table name don't change with de table name on the model #925

Closed ms1sistemas closed 1 year ago

ms1sistemas commented 1 year ago

Package version

18.3.0

Node.js and npm version

16.3.1

Sample Code (to reproduce the issue)

import { DateTime } from "luxon";
import {
  BaseModel,
  HasOne,
  ModelQueryBuilderContract,
  beforeCreate,
  beforeFetch,
  beforeFind,
  column,
  hasOne,
} from "@ioc:Adonis/Lucid/Orm";
import { v4 as uuidv4 } from "uuid";
import City from "./City";

export default class Supplier extends BaseModel {
  public static table = "people";

  @column({ isPrimary: true })
  public id: string;

  @column()
  public name: string;

   @column.dateTime({ autoCreate: true })
  public createdAt: DateTime;

  @column.dateTime({ autoCreate: true, autoUpdate: true })
  public updatedAt: DateTime;

  @hasOne(() => City, {
    localKey: "city_id",
    foreignKey: "id",

  })
  public city: HasOne<typeof City>;

Controller Suppliers

public async index({ request, auth }: HttpContextContract) {
    const { page, limit, order, type, search } = request.qs();
    try {
      const suppliers = Supplier.query()
        .select(
          "id",
          "name",

        )
        .preload("city", (query) => query.select("description"))  

      return await suppliers

    } catch (error) {
      throw new RecordNotFoundException(error);
    }

Error

RecordNotFoundException: Exception: Cannot preload "city", value of "Supplier.city_id" is undefined

preload don't use de table name on the supplier model