Skip to content

Support Postgres Composite TypesΒ #2895

@Huliiiiii

Description

@Huliiiiii

Discussed in #1816

Originally posted by wyatt-herkamp August 20, 2023
Support for Postgres Composite Types
As a note. I do not know a lot about databases. I am just starting to move my own projects from Mysql to Postgres due to its wide range of features.

Motivation

Using JSON for complex types is great because it allows for more compatibility for Mysql, Sqlite, and Postgres. However, if you are working on something that is Postgres only using the natively supported Composite Type would be a lot cleaner and nicer.

Proposed Solutions

Macro

A macro to auto implement any needed traits to do a composite type

/// A modified version of some code from one of my projects to show an example
#[derive(Debug, Clone, Serialize, PostgresCompositeType)]
#[sea_orm(type_name = "file_type")] // Optional
pub struct FileType {
    pub mime_type: String,
    pub charset: String,
    pub programming_language: Option<String>,
    pub content_language: Option<String>,
}
/// A modified version of some code from one of my projects to show an example
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)]
#[sea_orm(table_name = "paste_file")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i64,
    pub post_id: i64,
    pub file_name: String,
    pub file_type: FileType,
    pub content: String,
    #[sea_orm(default_expr = "Expr::current_timestamp()")]
    pub created: DateTimeWithTimeZone,
}

Traits That need to be implemeneted

  • FromValue

  • TryGetable

Other Changes

sea_orm::Value will also need a new Row type I think

This will also need any code needed to generate the Postgres Statement to make the type.

Documentation

We will also need to provide documenation on building composite types.

Current Workarounds

Using Json or using a seperate table.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions