Skip to content

Customize user ID field name? #1376

Answered by frankie567
JustinDigsData asked this question in Q&A
Discussion options

You must be logged in to vote

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:

  • Make a migration on your database to rename the column
  • Explicitly tell SQLAlchemy the name of the column:
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 id is an UUID, but it can be another thing, like a integer)

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@JustinDigsData
Comment options

Answer selected by JustinDigsData
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants