Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary column type is generated without blob size #1528

Closed
mattiekat opened this issue Mar 9, 2023 · 11 comments · Fixed by #1529 or #1548
Closed

Binary column type is generated without blob size #1528

mattiekat opened this issue Mar 9, 2023 · 11 comments · Fixed by #1529 or #1548
Assignees

Comments

@mattiekat
Copy link

Description

The auto-generated column trait implementation for entities does not specify a BlobSize.

Steps to Reproduce

I have a postgres table defined with a migration

.col(ColumnDef::new(Table::Column).binary())

that works without issue but then when I generate an entity using the sea-orm-cli it generates an invalid ColumnTrait implementation of

impl ColumnTrait for Column {
    type EntityName = Entity;
    fn def(&self) -> ColumnDef {
        match self {
            ...
            Self::Column => ColumnType::Binary.def().null(),
           ...
        }
    }
}

Expected Behavior

It should specify BlobSize::Blob(None) for this case (I believe), at a minimum code that compiles.

Actual Behavior

Generated a binary ColumnType without its required field value of BlobSize.

Reproduces How Often

Reliably

Workarounds

Manually edited the generated code. In this case it was a fairly minor update.

Reproducible Example

Non-trivial as it requires generating code from a live database.

Versions

postgres 15

├── sea-orm v0.11.0
│   ├── sea-orm-macros v0.11.0 (proc-macro)
│   ├── sea-query v0.28.3
│   │   ├── sea-query-derive v0.3.0 (proc-macro)
│   ├── sea-query-binder v0.3.0
│   │   ├── sea-query v0.28.3 (*)
│   ├── sea-strum v0.23.0
│   │   └── sea-strum_macros v0.23.0 (proc-macro)
├── sea-orm-migration v0.11.0
│   ├── sea-orm v0.11.0 (*)
│   ├── sea-orm-cli v0.11.0
│   │   ├── sea-schema v0.11.0
│   │   │   ├── sea-query v0.28.3 (*)
│   │   │   └── sea-schema-derive v0.1.0 (proc-macro)
│   ├── sea-schema v0.11.0 (*)
├── sea-orm v0.11.0 (*)
@tyt2y3
Copy link
Member

tyt2y3 commented Mar 9, 2023

Just to confirm, were you using the 0.11 version of the cli?

@billy1624 billy1624 self-assigned this Mar 9, 2023
@billy1624 billy1624 moved this from Triage to In Progress in SeaQL Dev Tracker Mar 9, 2023
@billy1624
Copy link
Member

Hey @mattiecnvr, thanks for the bug report!! I confirm it's reproducible on the latest sea-orm-cli. I just draft a fix for it, please check #1529

@mattiekat
Copy link
Author

Hey @mattiecnvr, thanks for the bug report!! I confirm it's reproducible on the latest sea-orm-cli. I just draft a fix for it, please check #1529

You are fast! Yep this does resolve it for my case. Thank you!

@billy1624
Copy link
Member

Thanks again for the filing! A patch release is on its way.

@tyt2y3
Copy link
Member

tyt2y3 commented Mar 10, 2023

Released in 0.11.1

@rtdavis22
Copy link

Hello, there still seems to be a problem if the --expanded-format flag is not given. The generated code includes this:

#[sea_orm(column_type = "Binary(BlobSize::Blob(None))", nullable)]

and I get the following error: "error[E0433]: failed to resolve: use of undeclared type BlobSize"

@billy1624
Copy link
Member

Hey @rtdavis22, thanks for catching this!

Solution:

  1. import sea_orm::sea_query::BlobSize into scope
use sea_orm::{entity::prelude::*, sea_query::BlobSize};

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "binary")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    #[sea_orm(column_type = "Binary(BlobSize::Blob(None))")]
    pub binary: Vec<u8>,
    #[sea_orm(column_type = "Binary(BlobSize::Blob(Some(10)))")]
    pub binary_10: Vec<u8>,
    #[sea_orm(column_type = "Binary(BlobSize::Tiny)")]
    pub binary_tiny: Vec<u8>,
    #[sea_orm(column_type = "Binary(BlobSize::Medium)")]
    pub binary_medium: Vec<u8>,
    #[sea_orm(column_type = "Binary(BlobSize::Long)")]
    pub binary_long: Vec<u8>,
    #[sea_orm(column_type = "VarBinary(10)")]
    pub var_binary: Vec<u8>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
  1. Or, fully quantify it as sea_orm::sea_query::BlobSize
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "binary")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    #[sea_orm(column_type = "Binary(sea_orm::sea_query::BlobSize::Blob(None))")]
    pub binary: Vec<u8>,
    #[sea_orm(column_type = "Binary(sea_orm::sea_query::BlobSize::Blob(Some(10)))")]
    pub binary_10: Vec<u8>,
    #[sea_orm(column_type = "Binary(sea_orm::sea_query::BlobSize::Tiny)")]
    pub binary_tiny: Vec<u8>,
    #[sea_orm(column_type = "Binary(sea_orm::sea_query::BlobSize::Medium)")]
    pub binary_medium: Vec<u8>,
    #[sea_orm(column_type = "Binary(sea_orm::sea_query::BlobSize::Long)")]
    pub binary_long: Vec<u8>,
    #[sea_orm(column_type = "VarBinary(10)")]
    pub var_binary: Vec<u8>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

Sorry for the inconvenience caused. We will patch it ASAP.

@rtdavis22
Copy link

Thanks!

@Sytten
Copy link
Contributor

Sytten commented Mar 15, 2023

@billy1624 The code generated by the CLI is throwing an error so thats not super great.
It should probably be added to the prelude.

@billy1624
Copy link
Member

billy1624 commented Mar 17, 2023

Binary(sea_orm::sea_query::BlobSize::Blob(None)) doesn't look good either. Re-exporting sea_query::BlobSize inside sea_orm::entity::prelude make sense to me.

@billy1624
Copy link
Member

Has been fixed on 0.11.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
5 participants