adonisjs / lucid

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

`.where` expects value to be defined, but undefined is passed #1069

Closed radiumrasheed closed 3 days ago

radiumrasheed commented 5 days ago

Package version

21.3.0

Describe the bug

TL:DR;

selfAssignPrimaryKey doesn't take into consideration of the naming strategy when finding the primary key value

Description

import { BaseSchema } from '@adonisjs/lucid/schema'

export default class extends BaseSchema {
  protected tableName = 'users'

  async up() {
    this.schema.createTable(this.tableName, (table) => {
      table.uuid('user_id').primary()

      table.timestamp('created_at')
      table.timestamp('updated_at')
    })
  }

  async down() {
    this.schema.dropTable(this.tableName)
  }
}
import { DateTime } from 'luxon'
import { BaseModel, beforeCreate, column } from '@adonisjs/lucid/orm'
import { randomUUID } from 'node:crypto'

export default class User extends BaseModel {
  static readonly selfAssignPrimaryKey = true

  @beforeCreate()
  static assignUuid(user: User) {
    user.userId = randomUUID()
  }

  @column({ isPrimary: true })
  declare userId: string

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

  @column.dateTime({ autoCreate: true, autoUpdate: true })
  declare updatedAt: DateTime
}
import router from '@adonisjs/core/services/router'
import User from '#models/user'
import { DateTime } from 'luxon'

router.get('/', async () => {
  const user = await User.create({})
  console.log(user.userId) // ✅ valid

  // trying to update any attribute fails
  user.createdAt = DateTime.now()
  await user.save() // 🚫 fails to update, as it's checking for `user.user_id` instead of `user.userId`
})

Reproduction repo

https://github.com/radiumrasheed/lucid-issue-1

thetutlage commented 5 days ago

The reproduction repo returns 404

radiumrasheed commented 4 days ago

The reproduction repo returns 404

my bad, just made it public @thetutlage

thetutlage commented 3 days ago

Thanks. The issue has been fixed and will be released soon 👍