Skip to content

Issue with created_at timestamp field not being correctly created in schema with default_factory. How do i add timestamp as a column in table in SQLmodel ? #1295

Closed Answered by YuriiMotov
srikrishnasajjarao asked this question in Questions
Discussion options

You must be logged in to vote

Can't reproduce the issue (see details)

import uuid
from datetime import datetime

from sqlmodel import Field, SQLModel, create_engine


class Job(SQLModel, table=True):
    id: str = Field(default_factory=lambda: str(uuid.uuid4()), primary_key=True)
    user_id: str
    created_at: datetime = Field(default_factory=datetime.utcnow)


engine = create_engine(
    "postgresql://user:mysecretpassword@localhost/some_db", echo=True
)
SQLModel.metadata.drop_all(engine)
SQLModel.metadata.create_all(engine)
CREATE TABLE job (
        id VARCHAR NOT NULL, 
        user_id VARCHAR NOT NULL, 
        created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL, 
        PRIMARY KEY (id)
)

To specify server-sid…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
2 participants