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

Problem with actions on database #1056

Closed
Mederick-ROBERT opened this issue Oct 10, 2024 · 8 comments
Closed

Problem with actions on database #1056

Mederick-ROBERT opened this issue Oct 10, 2024 · 8 comments

Comments

@Mederick-ROBERT
Copy link

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

Hey @Mederick-ROBERT! 👋🏻

Can you please share your User class?

@Mederick-ROBERT
Copy link
Author

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

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

sinh117801 commented Oct 11, 2024

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

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

@sinh117801
Copy link

@RomainLanz yes it works.

@FatihKaanAkkus
Copy link

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

Future discussion will happen here. adonisjs/core#4759

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

5 participants