Skip to content

Example of rust-sqlx/postgres usage with database enum types

License

Notifications You must be signed in to change notification settings

JohnBasrai/rust-sqlx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rust-sqlx

Example of how to insert and update a record with an enum column using rust-sqlx/postgresql. I could not find any examples of how to do the update operation (to an enum), but I found bits and pieces which I pulled together to write this demo. Hopefully it will save someone else some grief.


Running

Before running this demo against PostgreSQL (other databases supported by sqlx should also work with changes) we need to start an instance of PostgreSQL. The quickest way to do that to use an official Docker container image. For more information, please see docker-compose.yml.

docker run --rm --network=host --name=postgres -e POSTGRES_PASSWORD=welcome -e POSTGRES_USER=postgres postgres

From another shell window, exec into the database container to create the test table.

docker exec -i postgres psql --username=postgres < db-init.sql

Then build and run the code.

 $ export DATABASE_URL=postgres://postgres@localhost:5432/postgres
 $ cargo run
warning: unused manifest key: package.Authors
    Finished dev [unoptimized + debuginfo] target(s) in 0.08s
     Running `target/debug/rust-sqlx`
----- 1
----- 2
----- 3: Record { id: 1 }
----- 5

== SELECT users with sqlx::query_as!:

   User { id: 1, name: "Attila the long dead Hun:1", role: User }
----- 6

=== SELECT users with query.map..:
[User { id: 1, name: "Attila the long dead Hun:1", role: User }]
----- 7

=== SELECT users with query.map..:
[User { id: 1, name: "Attila the long dead Hun:1", role: User }]

That's it. If you run again you will see more rows added to the table. When the row count is greater than 10 it will delete 1 row for each row added.

About

Example of rust-sqlx/postgres usage with database enum types

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages