Crates.io | postgis_diesel |
lib.rs | postgis_diesel |
version | 2.4.1 |
source | src |
created_at | 2021-02-21 16:06:11.140921 |
updated_at | 2024-07-21 15:48:52.041312 |
description | An extension for Diesel framework to support PostGIS geometry datatype. |
homepage | https://github.com/vitaly-m/postgis-diesel |
repository | https://github.com/vitaly-m/postgis-diesel |
max_upload_size | |
id | 358554 |
size | 207,166 |
Extension for Diesel framework to support PostGIS types.
To ensure that the Geometry
type is in scope, read this guide and add postgis_diesel::sql_types::*
to the import_types key in your diesel.toml
file.
Assume that the table is defined like this:
CREATE EXTENSION IF NOT EXISTS postgis;
CREATE TABLE geometry_samples
(
id SERIAL PRIMARY KEY,
point geometry(Point,4326) NOT NULL,
linestring geometry(Linestring,4326) NOT NULL
);
Then Rust code may look like this:
#[macro_use]
extern crate diesel;
use postgis_diesel::operators::*;
use postgis_diesel::types::*;
#[derive(Insertable)]
#[diesel(table_name = geometry_samples)]
struct NewGeometrySample {
point: Point,
linestring: LineString<Point>,
}
#[derive(Queryable)]
struct GeometrySample {
id: i32,
point: Point,
linestring: LineString<Point>,
}
table! {
use postgis_diesel::sql_types::*;
use diesel::sql_types::*;
geometry_samples (id) {
id -> Int4,
point -> Geometry,
linestring -> Geometry,
}
}
See integration test for more complete example.
print-schema > src/full_schema.rs
.src/schema.rs
.diff -U6 src/full_schema.rs src/schema.rs > src/schema.patch
.patch_file = "src/schema.patch"
to diesel.toml.src/full_schema.rs
, check that diesel print-schema > src/schema.rs
will not add Geometry type.Example of patch file:
@@ -1,12 +1,9 @@
// @generated automatically by Diesel CLI.
pub mod sql_types {
- #[derive(diesel::query_builder::QueryId, diesel::sql_types::SqlType)]
- #[diesel(postgres_type(name = "geometry"))]
- pub struct Geometry;
#[derive(diesel::query_builder::QueryId, diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "intensity"))]
pub struct Intensity;
#[derive(diesel::query_builder::QueryId, diesel::sql_types::SqlType)]
@@ -52,13 +49,12 @@
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Intensity;
use super::sql_types::Triggermethod;
- use super::sql_types::Geometry;
laps (activity_id, started_at, manual_track) {
activity_id -> Uuid,
started_at -> Timestamptz,
total_time_seconds -> Float8,
distance_meters -> Float8,
docker compose up
cargo test