Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make a "timestamp with time zone"? #21

Closed
8 tasks done
typeshige opened this issue Aug 25, 2021 · 3 comments
Closed
8 tasks done

How to make a "timestamp with time zone"? #21

typeshige opened this issue Aug 25, 2021 · 3 comments
Labels
question Further information is requested

Comments

@typeshige
Copy link

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

import datetime

from typing import List, Optional

import sqlmodel


class AuthUser(sqlmodel.SQLModel, table=True):
  __tablename__ = 'auth_user'
  id: Optional[int] = sqlmodel.Field(default=None, primary_key=True)
  password: str = sqlmodel.Field(max_length=128)
  last_login: datetime.datetime

Description

I'm trying to make the last_login field become a "timestamp with time zone" field in Postgres.

With the above code, it is a "timestamp without time zone".

Operating System

Linux, macOS

Operating System Details

No response

SQLModel Version

0.0.3

Python Version

Python 3.8.1

Additional Context

No response

@typeshige typeshige added the question Further information is requested label Aug 25, 2021
@synic
Copy link

synic commented Aug 26, 2021

import datetime

from typing import List, Optional

import sqalchemy as sa
import sqlmodel


class AuthUser(sqlmodel.SQLModel, table=True):
  __tablename__ = 'auth_user'
  id: Optional[int] = sqlmodel.Field(default=None, primary_key=True)
  password: str = sqlmodel.Field(max_length=128)
  last_login: datetime.datetime = Field(sa_column=sa.Column(sa.DateTime(timezone=True), nullable=False))

@typeshige
Copy link
Author

Awesome, this was super helpful.

Since Column and DateTime are imported in sqlmodel, I modified it slightly as:


from typing import List, Optional

import sqlmodel


class AuthUser(sqlmodel.SQLModel, table=True):
  __tablename__ = 'auth_user'
  id: Optional[int] = sqlmodel.Field(default=None, primary_key=True)
  password: str = sqlmodel.Field(max_length=128)
  last_login: datetime.datetime = sqlmodel.Field(
      sa_column=sqlmodel.Column(
          sqlmodel.DateTime(timezone=True),
          nullable=False
      )
  )

@synic
Copy link

synic commented Aug 26, 2021

Nice. I kinda wish the pydantic fields (like EmailStr were also available in sqlmodel, so that everything is all in the same place) @tiangolo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants