Skip to content

How to refactor columns that needs sa_solumn attribute across multiple SQLModel tables? #954

Discussion options

You must be logged in to vote

Found the answer. Had to use sa_type. Here's the fixed code:

from datetime import datetime, timezone
from typing import Optional
from sqlmodel import SQLModel, Field, create_engine, Session
from sqlalchemy import Column, DateTime


def get_utc_now()->datetime:
    return datetime.now(timezone.utc)

class BaseModel(SQLModel):
    id:int=Field(primary_key=True)
    created_at:Optional[datetime]=Field(sa_type=DateTime(timezone=True),default_factory=get_utc_now)
    updated_at:Optional[datetime]=Field(sa_type=DateTime(timezone=True),default_factory=get_utc_now,sa_column_kwargs={'onupdate':get_utc_now})

class User(BaseModel,table=True):
    user_details:Optional[str]

class Product(BaseModel,…

Replies: 1 comment 1 reply

Comment options

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

Answer selected by vinodkumars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
1 participant