Typescript error on plugin schema reference field

When creating plugin for table schema, and adding reference to another table (e.g. user) I get TS error that instaed of reference it should be references
Object literal may only specify known properties, but 'reference' does not exist in type 'FieldAttribute<FieldType>'. Did you mean to write 'references'?ts(2561)
auth-C42JqOR9.d.ts(1893, 17): The expected type comes from property 'userId' which is declared here on type '{ [x: string]: FieldAttribute<FieldType>; }'
(property) reference: {
model: string;
field: string;
onDelete: string;
}
Object literal may only specify known properties, but 'reference' does not exist in type 'FieldAttribute<FieldType>'. Did you mean to write 'references'?ts(2561)
auth-C42JqOR9.d.ts(1893, 17): The expected type comes from property 'userId' which is declared here on type '{ [x: string]: FieldAttribute<FieldType>; }'
(property) reference: {
model: string;
field: string;
onDelete: string;
}
My code:
fields: {
userId: {
type: 'string',
required: true,
unique: true,
reference: {
model: 'user',
field: 'id',
onDelete: 'cascade',
},
},
fields: {
userId: {
type: 'string',
required: true,
unique: true,
reference: {
model: 'user',
field: 'id',
onDelete: 'cascade',
},
},
10 Replies
bekacru
bekacru2d ago
can you share your full plugin schema defination
Augustas Paliulis
const tosPlugin = () => {
return {
id: 'tos-plugin',
schema: {
TermsOfService: {
fields: {
userId: {
type: 'string',
required: true,
unique: true,
reference: {
model: 'user',
field: 'id',
onDelete: 'cascade',
},
},
acceptedAt: {
type: 'date',
required: true,
},
version: {
type: 'string',
required: true,
},
},
},
},
} satisfies BetterAuthPlugin;
};
const tosPlugin = () => {
return {
id: 'tos-plugin',
schema: {
TermsOfService: {
fields: {
userId: {
type: 'string',
required: true,
unique: true,
reference: {
model: 'user',
field: 'id',
onDelete: 'cascade',
},
},
acceptedAt: {
type: 'date',
required: true,
},
version: {
type: 'string',
required: true,
},
},
},
},
} satisfies BetterAuthPlugin;
};
bekacru
bekacru2d ago
oh it's references not reference
Augustas Paliulis
Is it? Because when I change to references I get an error during migration. With reference everything works find
bekacru
bekacru2d ago
what is the error?
Augustas Paliulis
can't tell you specifically, just that when using reference it is successful and also in documentation it's reference instead of references
UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "[object Array]".
at throwUnhandledRejectionsMode (node:internal/process/promises:392:7)
at processPromiseRejections (node:internal/process/promises:475:17)
at process.processTicksAndRejections (node:internal/process/task_queues:106:32) {
code: 'ERR_UNHANDLED_REJECTION'
}
UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "[object Array]".
at throwUnhandledRejectionsMode (node:internal/process/promises:392:7)
at processPromiseRejections (node:internal/process/promises:475:17)
at process.processTicksAndRejections (node:internal/process/task_queues:106:32) {
code: 'ERR_UNHANDLED_REJECTION'
}
bekacru
bekacru2d ago
maybe remove onDelete cascade incase that's the issue
Augustas Paliulis
Already tried that, it doesn't help
bekacru
bekacru2d ago
so npx @better-auth/cli migrate is failing when you add references?
Augustas Paliulis
Well at least for me. Only works when it is reference

Did you find this page helpful?