[Part 2] Database with SeaORM


3 min read

In this part, let us go through and setup the database with the necessary dependencies added to our project and create database, table & migration. Let's add the below shown dependencies in cargo.toml for adding SeaORM and dotenv.

name = "todo-actix-seaorm"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

sea-orm = { version = "^0", features = [ "sqlx-mysql", "runtime-actix-rustls", "macros" ], default-features = false }
dotenv = "0.15.0"
chrono = "0.4"
serde = "1"

Every crate ( dependency ) is hosted in crates.io can have features which are opt-in i.e We can choose the respective features to be included in our project and the build process efficiently takes only the opted feature's code.

In the above we have add sea-orm crate with features for mysql and actix runtime since we are going to connect the database in an actix service. Rust supports async functions and needs a runtime to manage. It is a whole different topic and before proceeding you can get a quick understanding of what async means in rust here.

The dotenv crate is used for loading environment variables from a .env file, if available, and mashes those with the actual environment variables provided by the operative system.

Chrono: It aims to be a feature-complete superset of the time library. In particular,

  • It strictly adheres to ISO 8601.
  • It is timezone-aware by default, with separate timezone-naive types.
  • It is space-optimal and (while not being the primary goal) reasonably efficient.

Serde is used for Serialization and De-serialization

Now that these two are added as dependencies, let's prepare our database.


Create a file called ".env" in the root of your project folder ( todo-actix-seaorm ) and add the below content. Replace with your connection details.

  • We will create the database rust_todo_actix_seaorm in a while using migrations.


We need the sqlx-cli for performing some migration commands. Let's install the sqlx-cli using the below command. Refer here for the complete usage and available options for sqlx-cli.

cargo install sqlx-cli

Database creation

Run the below command to create the database. Optionally you can also use your mysql client to create manually the database but make sure it is of the same name given in .env

sqlx database create

Initial Migration

Let's create the initial database schema setup. Run the below command to create a migration .sql file. You can substitute any name instead of "initial".

sqlx migrate add initial

It will create a file under "migrations/_initial.sql". Let's add MySQL statements to create our table. Copy the below contents and paste it in the .sql file.

USE rust_todo_actix_seaorm;

CREATE TABLE `todos` (
  `todo_id` bigint NOT NULL AUTO_INCREMENT,
  `todo_name` varchar(60) NOT NULL,
  `todo_description` varchar(150) DEFAULT NULL,
  PRIMARY KEY (`todo_id`),
  KEY `idx_todo_name_key` (`todo_name`)


We setup the database and the necessary migrations in place and can proceed with generation of entities in the next part. Until then, see ya ๐Ÿ‘‹๐Ÿป

Did you find this article valuable?

Support Omprakash Sridharan by becoming a sponsor. Any amount is appreciated!