I have the schema definition for postgres:
from sqlalchemy.orm import declarative_base
from services.db import DB
from sqlalchemy import Column, String, text, DateTime, ForeignKey
from sqlalchemy.orm import relationship
import uuid
Base = declarative_base()
class UserStatus(Base):
__tablename__ = 'user_statuses'
__table_args__ = {'schema': 'public'} # Specify the schema name
code = Column(String, primary_key=True)
label = Column(String, nullable=False)
users = relationship('User')
class User(Base):
__tablename__ = 'users'
__table_args__ = {'schema': 'public'} # Specify the schema name
id = Column(
String,
primary_key=True,
default=uuid.uuid4()
)
first_name = Column(String, nullable=False)
last_name = Column(String, nullable=False)
email = Column(String, nullable=False)
password = Column(String, nullable=False)
reset_token = Column(String, nullable=True)
status_code = Column(String, ForeignKey(
"user_statuses.code"))
created_at = Column(DateTime, nullable=False)
updated_at = Column(DateTime, nullable=True)
The problem is I keep getting error that the relation like this:
sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'users.status_code' could not find table 'user_statuses' with which to generate a foreign key to target column 'code'
i am using alembic for migration autogeneration and even if i create the tables from db and try to insert i get failure for the relationship.
What am I doing wrong here?
the code can be found here: https://github.com/rode093/fastapi-strawberry-graphql-template
in the branch defining-db-relationship