how to set server's date and time to addedat class OurDoc(Base): tablename_ = 'ourdoc'…
- User
how to set server's date and time to added_at class OurDoc(Base): tablename = 'our_doc'
doc_name: Mapped[str] = mapped_column(VARCHAR(32), unique=True, nullable=False) added_at: Mapped[datetime] = mapped_column(TIMESTAMP, nullable=False, server_default=func.now()) novalid_date: Mapped[datetime] = mapped_column(TIMESTAMP, nullable=False) - Assistant
To set the server's date and time to the added_at class in the OurDoc(Base) table, you need to use the SQLAlchemy’s func.now() function which returns the current date and time of the server. Here is the code that you can use to set the server's date and time to the added_at class:
from sqlalchemy import func class OurDoc(Base): __tablename__ = 'our_doc' doc_name: Mapped[str] = mapped_column(VARCHAR(32), unique=True, nullable=False) added_at: Mapped[datetime] = mapped_column(TIMESTAMP, nullable=False, server_default=func.now()) novalid_date: Mapped[datetime] = mapped_column(TIMESTAMP, nullable=False)In this code, the server_default parameter of the added_at column is set to the func.now() function, which means that every time a new row is inserted into the OurDoc table, the added_at column gets the current date and time of the server as its default value.
You can also use the SQL command to update the server's date and time manually. For example, if you want to set the added_at column to the current date and time, you can run the following SQL command:
UPDATE our_doc SET added_at = NOW() WHERE id = 1;This will update the added_at column of the row with the id=1 to the current date and time of the server.
- User
what means server_onupdate=func.now()
- Assistant
The
server_onupdateparameter in SQLAlchemy is used to specify a default value for a column that is automatically set by the database when an existing row is updated.The
func.now()function returns the current date and time of the server. Therefore, settingserver_onupdate=func.now()for a column means that every time a row is updated in the database, the value of that column for that row will be set to the current date and time of the server.Here's an example of using
server_onupdateto set the date and time of the last update for a column:class OurDoc(Base): __tablename__ = 'our_doc' doc_name: Mapped[str] = mapped_column(VARCHAR(32), unique=True, nullable=False) added_at: Mapped[datetime] = mapped_column(TIMESTAMP, nullable=False, server_default=func.now()) last_updated: Mapped[datetime] = mapped_column(TIMESTAMP, nullable=False, server_default=func.now(), server_onupdate=func.now())In this code, we have a
last_updatedcolumn that tracks the date and time of the last update for each row.server_default=func.now()sets the initial value of the column to the current date and time of the server, andserver_onupdate=func.now()sets the column value to the current date and time whenever the row is updated in the database. - User
what means if i do like this novalid_date: Mapped[datetime] = mapped_column(TIMESTAMP, default=None, server_onupdate=func.now())
- Assistant
If you define a column like this:
novalid_date: Mapped[datetime] = mapped_column(TIMESTAMP, default=None, server_onupdate=func.now()), it means that thenovalid_datecolumn will have a default value ofNoneand its value will be updated automatically to the current date and time of the server every time the row is updated in the database.This can be useful when you want to track when a row was last updated, but don't have a specific default value for the
novalid_datecolumn. By setting thedefaultparameter toNone, the column's value will beNoneif not provided when a new row is inserted. When the row is updated, theserver_onupdateparameter will automatically set thenovalid_datecolumn's value to the current date and time of the server.Here's an example of using
default=Noneandserver_onupdateto set thenovalid_datecolumn to track the date and time of the last update:class OurDoc(Base): __tablename__ = 'our_doc' doc_name: Mapped[str] = mapped_column(VARCHAR(32), unique=True, nullable=False) added_at: Mapped[datetime] = mapped_column(TIMESTAMP, nullable=False, server_default=func.now()) novalid_date: Mapped[datetime] = mapped_column(TIMESTAMP, default=None, server_onupdate=func.now())In this code, we have a
novalid_datecolumn that tracks the date and time of the last update for each row. Sincedefault=None, its value will beNoneif not provided during the row insertion. When the row is updated, theserver_onupdateparameter will automatically set the column value to the current date and time of the server.