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

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

Closed
erick-ciudaz opened this issue Oct 13, 2024 · 3 comments

Comments

@erick-ciudaz
Copy link

erick-ciudaz commented Oct 13, 2024

Package version

@adonisjs/[email protected]

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:

  • Node.js version: 22.9.0
  • AdonisJS version: Latest (configured with npm init adonisjs@latest)
  • @adonisjs/lucid 21.3.0
  • Database: PostgreSQL
  • Operating System: Ubuntu 22.04.4 LTS
  • Additional setup: Project configured with Inertia.js, Vue.js, and Server-Side Rendering (SSR) enabled.

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: '[email protected]',
      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
Copy link
Member

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

@RomainLanz Thanks for the quick reply. I was using @swc/[email protected], but when I switched to 1.7.26, I was able to run the seeders correctly.

@thetutlage
Copy link
Member

Closing since not actionable

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

3 participants