Your question
First of all, i am really sorry but I am bit new to next/nextauth. I started by cloning the example project and went from there. I have one enabled provider (discord). My issue is that everything works fine when i am running the dev build but as soon as I am running the production build (next start using npm or yarn build then start), users cannot login. I am getting "QueryFailedError: relation "account__accounts" does not exist"
What are you trying to do
Here's a snippet from my simple config:
import Models from "../../../models";
import Adapters from "next-auth/adapters"
const options = {
providers: [
Providers.Discord({
clientId: process.env.DISCORD_CLIENT_ID,
clientSecret: process.env.DISCORD_CLIENT_SECRET,
scope: 'identify email guilds',
}),
],
adapter: Adapters.TypeORM.Adapter(
// The first argument should be a database connection string or TypeORM config object
process.env.DATABASE_URL,
// The second argument can be used to pass custom models and schemas
{
models: {
User: Models.User,
Account: Models.Account,
},
}
)
When I am running yarn dev, everything is working as expected.
When I am switching to yarn build & yarn start using the same .env from the same machine, i am getting the following exception:
[next-auth][error][get_user_by_provider_account_id_error] QueryFailedError: relation "account__accounts" does not exist
at new QueryFailedError (/Users/user/Downloads/project/node_modules/typeorm/error/QueryFailedError.js:11:28)
at Query.callback (/Users/user/Downloads/project/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:184:38)
at Query.handleError (/Users/user/Downloads/project/node_modules/pg/lib/query.js:139:19)
at Client._handleErrorMessage (/Users/user/Downloads/project/node_modules/pg/lib/client.js:326:17)
at Connection.emit (events.js:315:20)
at /Users/user/Downloads/project/node_modules/pg/lib/connection.js:109:12
at Parser.parse (/Users/user/Downloads/project/node_modules/pg-protocol/dist/parser.js:40:17)
at Socket.<anonymous> (/Users/user/Downloads/project/node_modules/pg-protocol/dist/index.js:8:42)
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:295:12) {
length: 117,
severity: 'ERROR',
code: '42P01',
detail: undefined,
hint: undefined,
position: '752',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'parse_relation.c',
line: '1194',
routine: 'parserOpenTable',
query: 'SELECT "Account_Account"."id" AS "Account_Account_id", "Account_Account"."compound_id" AS "Account_Account_compound_id", "Account_Account"."user_id" AS "Account_Account_user_id", "Account_Account"."provider_type" AS "Account_Account_provider_type", "Account_Account"."provider_id" AS "Account_Account_provider_id", "Account_Account"."provider_account_id" AS "Account_Account_provider_account_id", "Account_Account"."refresh_token" AS "Account_Account_refresh_token", "Account_Account"."access_token" AS "Account_Account_access_token", "Account_Account"."access_token_expires" AS "Account_Account_access_token_expires", "Account_Account"."created_at" AS "Account_Account_created_at", "Account_Account"."updated_at" AS "Account_Account_updated_at" FROM "account__accounts" "Account_Account" WHERE "Account_Account"."provider_id" = $1 AND "Account_Account"."provider_account_id" = $2 LIMIT 1',
parameters: [ 'discord', '210621734712377344' ]
}
https://next-auth.js.org/errors#get_user_by_provider_account_id_error
[next-auth][error][oauth_callback_handler_error] Error: GET_USER_BY_PROVIDER_ACCOUNT_ID_ERROR
at /Users/user/Downloads/project/node_modules/next-auth/dist/adapters/typeorm/index.js:224:35
at Generator.throw (<anonymous>)
at asyncGeneratorStep (/Users/user/Downloads/project/node_modules/next-auth/dist/adapters/typeorm/index.js:28:103)
at _throw (/Users/user/Downloads/project/node_modules/next-auth/dist/adapters/typeorm/index.js:30:291)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
https://next-auth.js.org/errors#oauth_callback_handler_error
Now i understand from https://next-auth.js.org/errors that GET_USER_BY_PROVIDER_ACCOUNT_ID_ERROR means a DB interaction error but what I am failing to understand is why I am getting this error if I am running the build using the same .env and using the same db instance. I am using a postgres instance if that helps. I appreciate any assistance.
Feedback
Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.
Hi there!
Hmm I wonder if you have a slightly different schema on production database than in development database (assuming you have more than one database)?
If you are able to login in with a tool you can use to view and compare schemas on each database that might highlight any discrepancies.
If you only have one database and are connecting to it in both development and production modes I'd be surprised to see this, but do let me know if that's the case!
Hi ian,
Indeed i tested by connecting the production build to the same DB i am using in the dev environment and I was still facing this issue. As a workaround I created two new views account_accounts and user_users in the db. For some reason in the production build, next-auth is trying to query account_accounts and user_users instead of accounts and users when using custom models.
Thanks for the response! Hmm that is odd and I don't know why that is happening.
It seems like it would be helpful to have this open to look at and for us to try and work out what is going on.
Im seeing the same issue: //Simplifying
class User extends Adapters.TypeORM.Models.User.model {
constructor(name, email, image, emailVerified) {
super(name, email, image, emailVerified)
}
}
Running yarn dev gets:
User = [Function: User]
Running with build / start (OR deploying to Vercel) gets:
User = [Function: User_User]
We have the exact same issue, in dev mode it works perfectly, but on production mode it duplicates the table and you get users_users, etc.
Looking very quickly to the code, why is https://github.com/nextauthjs/next-auth/blob/main/src/adapters/typeorm/index.js#L72 needed on non production envs only? I think it might be related to the issue 馃
Edit
I just tried removing the environment check locally and that worked perfectly
if (process.env.NODE_ENV !== 'production') {
await updateConnectionEntities(connection, config.entities)
}
Since synchronising the schema is dangerous and removing that if statement is not possible without forking the library I tried simply writing the model/schema from scratch instead of extending the provided model. This works for me and it is the only workaround I could find.
If you have a custom model, for example you add a role column to the users table
import Adapters from 'next-auth/adapters';
// Extend the built-in models using class inheritance
export default class User extends Adapters.TypeORM.Models.User.model {
// You can extend the options in a model but you should not remove the base
// properties or change the order of the built-in options on the constructor
constructor(name, email, image, emailVerified) {
super(name, email, image, emailVerified);
}
}
export const UserSchema = {
name: 'User',
target: User,
columns: {
...Adapters.TypeORM.Models.User.schema.columns,
role: {
type: 'varchar',
nullable: true,
},
},
};
instead do it without extending the class
export default class User {
constructor(name, email, image, emailVerified) {
if (name) {
this.name = name;
}
if (email) {
this.email = email;
}
if (image) {
this.image = image;
}
if (emailVerified) {
const currentDate = new Date();
this.emailVerified = currentDate;
}
}
}
export const UserSchema = {
name: 'User',
target: User,
columns: {
id: {
primary: true,
type: 'int',
generated: true,
},
name: {
type: 'varchar',
nullable: true,
},
email: {
type: 'varchar',
unique: true,
nullable: true,
},
emailVerified: {
type: 'timestamp',
nullable: true,
},
image: {
type: 'varchar',
nullable: true,
},
createdAt: {
type: 'timestamp',
createDate: true,
},
updatedAt: {
type: 'timestamp',
updateDate: true,
},
role: {
type: 'varchar',
nullable: true,
},
},
};
this "solves" the problem.
I can confirm I have the same issue and @fedekau "solution" works
got into this too; I think @fedekau's hot fix work for many cases! But in my case I have another couple of api calls using the TypeORM adaptor to perform CURD operations with custom models, and I'm inconsistently getting some weird EntityMetadataNotFoundError (only in serverless setting; works perfectly locally & on heroku! ) : - /
could be related to this:
https://github.com/typeorm/typeorm/issues/1327
maybe something to do with the constraint in a serverless setting and some of the stuff in typeORM weren't implemented with serverless in mind?
Hmm or could also be due to some optimsiation done in Vercel for the functional calls I guess. just opened a discussion here: https://github.com/vercel/vercel/discussions/5299 Really hoping to get this fixed before we launch our app : 0
@archywillhe Another way around this for now if your having problems is to manually rename it:
class User extends Adapters.TypeORM.Models.User.model {
// You can extend the options in a model but you should not remove the base
// properties or change the order of the built-in options on the constructor
constructor(name, email, image, emailVerified) {
super(name, email, image, emailVerified)
}
}
Object.defineProperty(User, "name", { value: "User" })
@glenames ohh interesting! I will give it a try! thanks mate!
also: dou you know where can i read more about using .defineProperty on Classes?
update: unforuntaely still getting Error?error=EntityMetadataNotFound%3A%20No%20metadata%20for%20"User"%20was%20found.
Hi there! It looks like this issue hasn't had any activity for a while. It will be closed if no further activity occurs. If you think your issue is still relevant, feel free to comment on it to keep it open. (Read more at #912) Thanks!
Hi there! It looks like this issue hasn't had any activity for a while. To keep things tidy, I am going to close this issue for now. If you think your issue is still relevant, just leave a comment and I will reopen it. (Read more at #912) Thanks!
Most helpful comment
Hi ian,
Indeed i tested by connecting the production build to the same DB i am using in the dev environment and I was still facing this issue. As a workaround I created two new views account_accounts and user_users in the db. For some reason in the production build, next-auth is trying to query account_accounts and user_users instead of accounts and users when using custom models.