adonisjs / lucid

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

Error when executing seeder with fullName column in a fresh AdonisJS project #1058

Closed erick-ciudaz closed 1 month ago

erick-ciudaz commented 1 month ago

Package version

@adonisjs/lucid@21.3.0

Describe the bug

I'm experiencing an issue with Lucid ORM in a new AdonisJS project when trying to execute a seeder that registers a user. The error occurs because Lucid cannot define the fullName property on the User model, even though it is correctly defined in both the migration and the model.

Environment:

Migration:

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

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

  async up() {
    this.schema.createTable(this.tableName, (table) => {
      table.increments('id').notNullable()
      table.string('full_name').nullable()
      table.string('email', 254).notNullable().unique()
      table.string('password').notNullable()
      table.timestamp('created_at').notNullable()
      table.timestamp('updated_at').nullable()
    })
  }
}

Model

import { DateTime } from 'luxon'
import { BaseModel, column } from '@adonisjs/lucid/orm'
import { withAuthFinder } from '@adonisjs/auth'
import { compose } from '@adonisjs/core/helpers'
import hash from '@adonisjs/core/services/hash'

const AuthFinder = withAuthFinder(() => hash.use('scrypt'), {
  uids: ['email'],
  passwordColumnName: 'password',
})

export default class User extends compose(BaseModel, AuthFinder) {
  @column({ isPrimary: true })
  declare id: number

  @column()
  declare fullName: string | null

  @column()
  declare email: string

  @column({ serializeAs: null })
  declare password: string

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

  @column.dateTime({ autoCreate: true, autoUpdate: true })
  declare updatedAt: DateTime | null
}

Seeder

import User from '#models/user'
import { BaseSeeder } from '@adonisjs/lucid/seeders'
import { DateTime } from 'luxon'

export default class UserSeeder extends BaseSeeder {
  public async run() {
    User.create({
      fullName: 'TestUser',
      email: 'testuser@gmail.com',
      password: '123456789',
      updatedAt: DateTime.now(),
      createdAt: DateTime.now(),
    })
  }
}

Error

When I run the migration and then execute the seeders, I encounter the following error:

[ success ] Dropped tables successfully
[ info ] Upgrading migrations version from "1" to "2"
❯ migrated database/migrations/1728592522466_create_users_table

Migrated in 72 ms
Map(0) {}
❯ completed database/seeders/user_seeder
file:///home/Gampel/Documentos/test/adonisjsTest/node_modules/@adonisjs/lucid/build/src/orm/base_model/index.js:1289
                    throw new Error(`Cannot define "${key}" on "${Model.name}" model, since it is not defined as a model property`);
                          ^

Error: Cannot define "fullName" on "User" model, since it is not defined as a model property
    at file:///home/Gampel/Documentos/test/adonisjsTest/node_modules/@adonisjs/lucid/build/src/orm/base_model/index.js:1289:27
    at Array.forEach (<anonymous>)
    at Proxy.merge (file:///home/Gampel/Documentos/test/adonisjsTest/node_modules/@adonisjs/lucid/build/src/orm/base_model/index.js:1248:33)
    at User.newUpWithOptions (file:///home/Gampel/Documentos/test/adonisjsTest/node_modules/@adonisjs/lucid/build/src/orm/base_model/index.js:118:13)
    at User.create (file:///home/Gampel/Documentos/test/adonisjsTest/node_modules/@adonisjs/lucid/build/src/orm/base_model/index.js:437:31)
    at UserSeeder.run (file:///home/Gampel/Documentos/test/adonisjsTest/database/seeders/user_seeder.ts:9:14)
    at SeedsRunner.run (file:///home/Gampel/Documentos/test/adonisjsTest/node_modules/@adonisjs/lucid/build/src/seeders/runner.js:66:34)
    at async DbSeed.executedSeeders (file:///home/Gampel/Documentos/test/adonisjsTest/node_modules/@adonisjs/lucid/build/commands/db_seed.js:119:30)
    at async DbSeed.runAsSubCommand (file:///home/Gampel/Documentos/test/adonisjsTest/node_modules/@adonisjs/lucid/build/commands/db_seed.js:178:24)
    at async DbSeed.run (file:///home/Gampel/Documentos/test/adonisjsTest/node_modules/@adonisjs/lucid/build/commands/db_seed.js:201:13)

It seems like Lucid cannot register the fullName property correctly, even though it's defined in both the migration and the model.

RomainLanz commented 1 month ago

Hey @erick-ciudaz! 👋🏻

What version of @swc/core are you using? They pushed a broken release lately that may affect decorators. Try to downgrade it to 1.7.26.

erick-ciudaz commented 1 month ago

@RomainLanz Thanks for the quick reply. I was using @swc/core@1.7.35, but when I switched to 1.7.26, I was able to run the seeders correctly.

thetutlage commented 1 month ago

Closing since not actionable