-
|
Hi, I’m trying to figure out how to create a record with a related object using FastCRUD. DBModel class BaseSQL(DeclarativeBase):
__abstract__ = True
@declared_attr.directive
def id(cls) -> Mapped[str]:
return mapped_column(String(36), primary_key=True, default=lambda: str(uuid4()))
@declared_attr.directive
def created_at(cls) -> Mapped[datetime]:
return mapped_column(
DateTime(timezone=True),
default=lambda: datetime.now(timezone.utc),
nullable=False,
)
@declared_attr.directive
def updated_at(cls) -> Mapped[datetime]:
return mapped_column(
DateTime(timezone=True),
nullable=True,
onupdate=lambda: datetime.now(timezone.utc),
)
class CrawlerAccountOAuthSQL(BaseSQL):
...
oauth: Mapped["CrawlerAccountOAuthSQL | None"] = relationship(
back_populates="crawler_account",
uselist=False,
cascade="all, delete-orphan",
)
class CrawlerAccountOAuthSQL(BaseSQL):
...
crawler_account: Mapped["CrawlerAccountSQL"] = relationship(
back_populates="oauth",
)With plain SQLAlchemy + async session, I can create a CrawlerAccountSQL along with its related CrawlerAccountOAuthSQL like this: async with async_session_maker() as session:
session.add(
CrawlerAccountSQL(
account="[email protected]",
password="123",
status=CrawlerAccountStatusEnum.ACTIVE,
oauth=CrawlerAccountOAuthSQL(
oauth_name="google",
account_email="[email protected]",
account_password="[email protected]",
),
)
)
await session.commit()This works fine. But when I try to do the same thing using FastCRUD, I get an error: class CrawlerAccountOAuthCreate(BaseModel):
oauth_name: str
account_email: EmailStr
account_password: str
class CrawlerAccountSQLCreate(BaseModel):
account: str
comment: str | None
proxy_id: str | None
password: str
status: CrawlerAccountStatusEnum
oauth: CrawlerAccountOAuthCreate
crud = FastCRUD(model=CrawlerAccountSQL)
await crud.create(
db=session,
object=CrawlerAccountSQLCreate(
account="[email protected]",
password="123",
proxy_id=None,
comment="",
oauth=CrawlerAccountOAuthDBCreate(
oauth_name="google",
account_email="[email protected]",
account_password="[email protected]",
),
),
)This raises:
Is it not possible to perform a relational create? Do I have to insert them separately instead? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Hey @hgalytoby! |
Beta Was this translation helpful? Give feedback.
Hey @hgalytoby!
Currently it's not supported, but it's actually a good issue, I'll convert