Reflex Cloud - Fast, secure & scalable hosting. One command to deploy.

Tables

Tables are database objects that contain all the data in a database.

In tables, data is logically organized in a row-and-column format similar to a spreadsheet. Each row represents a unique record, and each column represents a field in the record.

Creating a Table

To create a table make a class that inherits from rx.Model.

The following example shows how to create a table called User.

The table=True argument tells Reflex to create a table in the database for this class.

Primary Key

By default, Reflex will create a primary key column called id for each table.

However, if an rx.Model defines a different field with primary_key=True, then the default id field will not be created. A table may also redefine id as needed.

It is not currently possible to create a table without a primary key.

Advanced Column Types

SQLModel automatically maps basic python types to SQLAlchemy column types, but for more advanced use cases, it is possible to define the column type using sqlalchemy directly. For example, we can add a last updated timestamp to the post example as a proper DateTime field with timezone.

To make the Post model more usable on the frontend, a dict method may be provided that converts any fields to a JSON serializable value. In this case, the dict method is overriding the default datetime serializer to strip off the microsecond part.