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

Problem with actions on database #1056

Closed Mederick-ROBERT closed 1 month ago

Mederick-ROBERT commented 1 month ago

Package version

21.2.0

Describe the bug

I started a new project with the API Starter Kit and access_tokens guard + a Postgres db. I connect the db in .env file, realised migrations with success. After I create a new seeder for create a new user, I have this error :

 node ace db:seed -i
❯ Select files to run · database/seeders/user_seeder
❯ error     database/seeders/user_seeder
  Cannot define "email" on "User" model, since it is not defined as a model property

The seeder :

import { BaseSeeder } from '@adonisjs/lucid/seeders'
import User from "#models/user";

export default class UserSeeder extends BaseSeeder {
  async run() {
    await User.create({
      email: 'test@test.fr',
      password: 'password',
    })
  }
} 

I test with the same things but without the access token guard and I have this new error :

 node ace db:seed -i
❯ Select files to run · database/seeders/user_seeder
❯ error     database/seeders/user_seeder
  Cannot read properties of undefined (reading 'get')

After that, I create a GET route with a json response for getting all users, and I get this error :

 ERROR (28175): Invalid or unexpected token
    request_id: "fx7golfn5vc4e89vaijwdqdw"
    x-request-id: "fx7golfn5vc4e89vaijwdqdw"
    err: {
      "type": "SyntaxError",
      "message": "Invalid or unexpected token",
      "stack":
          SyntaxError: Invalid or unexpected token
              at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:152:18)
              at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:299:14)
              at async link (node:internal/modules/esm/module_job:67:21)
      "status": 500
    }

I don't know if it's a bug, or if I configured my project incorrectly

Reproduction repo

No response

RomainLanz commented 1 month ago

Hey @Mederick-ROBERT! 👋🏻

Can you please share your User class?

Mederick-ROBERT commented 1 month ago

Yes of course, I didn't change anything on this after the install :

With the acces token guard :

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

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

  static accessTokens = DbAccessTokensProvider.forModel(User)
}

without the guard :

import { DateTime } from 'luxon'
import { BaseModel, column } from '@adonisjs/lucid/orm'

export default class User extends BaseModel {
  @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
}
FatihKaanAkkus commented 1 month ago

I have the same issue with a similar User model. Only thing I changed is the prop name email to username in model and migration files. Package version is 21.2.0. Created project with adonis api starter project.

sinh117801 commented 1 month ago

I got the same issue with db:seed. I wonder why it select from "user_with_user_finders" where it doesn't event exists. I updated @adonisjs/lucid to 21.3.0 and @swc/core to 1.7.35

❯ completed database/seeders/main/index_seeder
"postgres" User (3.37 ms) SELECT * FROM "user_with_user_finders" WHERE "username" = ? LIMIT ? FOR UPDATE [ 'admin', 1 ]
❯ error     database/seeders/main/user_seeder
  select * from "user_with_user_finders" where "username" = $1 limit $2 for update - relation "user_with_user_finders" does not exist
// my seed file
export default class UserSeeder extends BaseSeeder {
  async run() {
    await User.updateOrCreate({
      username: 'admin',
    }, {
      password: 'password'
    });
  }
}

// my model

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

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

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

  @column()
  declare username: string;

  @column()
  declare password: string;

  @column()
  declare isActive: boolean;

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

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

  static accessTokens = DbAccessTokensProvider.forModel(User, {
    expiresIn: '1h',
  });
}
RomainLanz commented 1 month ago

Can you try to downgrade @swc/core to 1.7.26?

sinh117801 commented 1 month ago

@RomainLanz yes it works.

FatihKaanAkkus commented 1 month ago

Can you try to downgrade @swc/core to 1.7.26?

Downgrade to this version works. I had to manually replace in my lock file for some reason, but thats okay. ^_^

thetutlage commented 1 month ago

Future discussion will happen here. https://github.com/adonisjs/core/issues/4759