SeaQL / sea-orm

🐚 An async & dynamic ORM for Rust
https://www.sea-ql.org/SeaORM/
Apache License 2.0
6.96k stars 486 forks source link

mismatched types; Rust type `core::option::Option<alloc::string::String>` (as SQL type `VARCHAR`) is not compatible with SQL type `VARBINARY` #704

Closed ttys3 closed 2 years ago

ttys3 commented 2 years ago

Description

create table with field like varbinary(20) will result in error with find_by_id(id)

error occurred while decoding column
mismatched types; Rust type `core::option::Option<alloc::string::String>` (as SQL type `VARCHAR`) is not compatible with SQL type `VARBINARY`

Steps to Reproduce

  1. create table demo which has a varbinary(20) field
CREATE TABLE `demo` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `secret` varbinary(20) NOT NULL,
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8
  1. insert some demo data:
INSERT INTO demo (secret) VALUES (UNHEX('b2df817c8b751a3ae3d8ba1de4dc8369'));

select * from demo;
+----+------------------------------------+
| id | secret                             |
+----+------------------------------------+
| 1  | 0xb2df817c8b751a3ae3d8ba1de4dc8369 |
+----+------------------------------------+
  1. generate entity using cli: sea-orm-cli generate entity -t demo

and we'll got

//! SeaORM Entity. Generated by sea-orm-codegen 0.8.0

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "demo")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: u32,
    #[sea_orm(column_type = "Custom(\"VARBINARY(20)\".to_owned())")]
    pub secret: String,
}

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

impl RelationTrait for Relation {
    fn def(&self) -> RelationDef {
        panic!("No RelationDef")
    }
}

impl ActiveModelBehavior for ActiveModel {}
  1. fetch one row data:
use super::demo::Entity as Demo;

let hello: Option<demo::Model> = Demo::find_by_id(1).one(db).await?;

and we got the errror "mismatched types; Rust type core::option::Option (as SQL type VARCHAR) is not compatible with SQL type VARBINARY"

Expected Behavior

sea-orm-cli generate entity with table has VARBINARY fields execute find_by_id query has no problem

Actual Behavior

sea-orm-cli generate entity with table has VARBINARY fields execute find_by_id query got "mismatched types; Rust type core::option::Option (as SQL type VARCHAR) is not compatible with SQL type VARBINARY"

Reproduces How Often

always reproducible

Versions

Additional Information

ikrivosheev commented 2 years ago

Hello, @ttys3! Thank you for the report. Can write a small example which reproduces error?

ikrivosheev commented 2 years ago

And I think varbinary is not equal to String. You can use Binary instead of String.

billy1624 commented 2 years ago

Hey @ttys3, I think varbinary should represented as Vec<u8> in Rust

ttys3 commented 2 years ago

@ikrivosheev @billy1624 I updated the issue with re-produce steps.

I think the problem is sea-orm-cli generate entity does not support table has VARBINARY fields

ikrivosheev commented 2 years ago

Ah... I understand, problem is in generator. Thank you for the report.