Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
radiumrasheed opened this issue Nov 24, 2024 · 3 comments
Closed

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

radiumrasheed opened this issue Nov 24, 2024 · 3 comments

Comments

@radiumrasheed
Copy link
Contributor

radiumrasheed commented Nov 24, 2024

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
Copy link
Member

The reproduction repo returns 404

@radiumrasheed
Copy link
Contributor Author

radiumrasheed commented Nov 25, 2024

The reproduction repo returns 404

my bad, just made it public @thetutlage

@thetutlage
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants