Customize user ID field name? #1376
-
Hi, I'm trying to port code and an existing database over to using FastAPI Users for authentication and am running into one small issue around the name of the ID column. My existing user database's ID column is called "user_id" instead of just "id." Is there a way to override the default ID column name? Right now, I'm getting an AttributeError when I attempt to use that column in my User class:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Just saw this note: #1245 Assuming it's still the case that the name of the User id field just is what it is? |
Beta Was this translation helpful? Give feedback.
-
Well, if it's just the name of the column in the database; but it's okay for you to use
import uuid
from fastapi_users.db import SQLAlchemyBaseUserTable
from fastapi_users_db_sqlalchemy.generics import GUID
from sqlalchemy.orm import Mapped, mapped_column
class User(SQLAlchemyBaseUserTable[uuid.UUID], Base):
id: Mapped[uuid.UUID] = mapped_column(
"user_id", GUID, primary_key=True, default=uuid.uuid4
) (assuming your |
Beta Was this translation helpful? Give feedback.
Well, if it's just the name of the column in the database; but it's okay for you to use
id
in the Python code, you can either:(assuming your
id
is an UUID, but it can be another thing, like a integer)