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

Sanctum Cannot Find Existing Database Table When Using Sanctum::usePersonalAccessTokenModel() #555

Open
onairmarc opened this issue Jan 19, 2025 · 9 comments

Comments

@onairmarc
Copy link

Sanctum Version

4.0.7

Laravel Version

11.38.2

PHP Version

8.3

Database Driver & Version

MySQL 8

Description

When customizing the personal_access_tokens table name via a Model Override per the documentation and then making a request to an API, an error is displayed stating SQLSTATE[42S02]: Base table or view not found: 1146 Table 'servicepoint_sp1_dev.personal_access_tokens' doesn't exist (Connection: mysql, SQL: select * from personal_access_tokenswherepersonal_access_tokens.id = 1 limit 1)

The odd thing is that if I change the table name from my custom name of sys_personal_access_tokens back to the default of personal_access_tokens the issue remains, except now Sanctum is complaining that it cannot find the database table with the custom name.

The only way I've been able to workaround this issue is by changing the database table name back to personal_access_tokens and removing Sanctum::usePersonalAccessTokenModel(CustomPersonalAccessToken::class); from my AppServiceProvider

Steps To Reproduce

  1. Create a model that extends the Sanctum PersonalAccessToken Model.
  2. Set the custom table name on the custom model.
  3. Override the model in your AppServiceProvider with Sanctum::usePersonalAccessTokenModel(CustomPersonalAccessToken::class);
  4. Make a request to any of your endpoints.
  5. See the error.
  6. Rename the table manually from sys_personal_access_tokens to personal_access_tokens
  7. Make another request and see the same error, except Sanctum now complains that the customized name is missing.
  8. Comment out the model override and make a request with a successful response.
@onairmarc onairmarc changed the title Sanctum Cannot FInd Existing Database Table When Using Sanctum::usePersonalAccessTokenModel() Sanctum Cannot Find Existing Database Table When Using Sanctum::usePersonalAccessTokenModel() Jan 19, 2025
@crynobone
Copy link
Member

Hey there, thanks for reporting this issue.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here?

Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

@onairmarc
Copy link
Author

onairmarc commented Jan 20, 2025

@crynobone Just created and pushed the repo. I tested it locally using SQLite and could not reproduce the issue. I then tested it gain using a MySQL database and was able to reproduce the issue. Please let me know if you need anything more from me.

https://github.com/onairmarc/laravel-sanctum-bug-report-555

@crynobone
Copy link
Member

Will check this out tomorrow

@onairmarc
Copy link
Author

Thank you! There's no rush. We made the necessary production changes to accommodate this bug in the meantime, but we eventually would like to restore our naming convention rather than having a single stray table. Appreciate your help!

@crynobone
Copy link
Member

I then tested it gain using a MySQL database and was able to reproduce the issue. Please let me know if you need anything more from me.

How should I run the app to generate the error. It doesn't seem to provide any seeds to generate rows to sys_personal_access_tokens table

@onairmarc
Copy link
Author

I created the user and the token using Tinker in the terminal. Let me know if you want me to add seeders to the repo.

@crynobone
Copy link
Member

crynobone commented Jan 22, 2025

I created the user and the token using Tinker in the terminal. Let me know if you want me to add seeders to the repo.

If you can create the token, doesn't that mean everything working correctly? A seeder would be good here

@onairmarc
Copy link
Author

Normally, yes, I would say everything is working, but this issue arises when an API request is received by the application, not when the token is being created. It's odd behavior.

I'll have time to create the seeder this afternoon.

@onairmarc
Copy link
Author

Just pushed the creation of the API Token to the seeder.

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

No branches or pull requests

2 participants