diesel-rs / diesel_full_text_search

MIT License
72 stars 33 forks source link

why the rust diesel did not throw error when execute the error syntex sql in postgresql 13 #32

Closed jiangxiaoqiang closed 2 years ago

jiangxiaoqiang commented 2 years ago

I am using rust (v1.62) diesel to do a full text search, now I found a problem that when I pass the wrong sql to PostgreSQL, the rust diesel app did not throw error, that's makes me confusing. This is the main.rs:

#[macro_use]
extern crate diesel;

use diesel::{debug_query, ExpressionMethods, QueryDsl, QueryResult, RunQueryDsl};
use diesel::query_builder::BoxedSelectStatement;
use diesel_full_text_search::{to_tsquery, to_tsvector};
use diesel_full_text_search::TsVectorExtensions;
use rust_wheel::config::db::config;
use crate::model::diesel::dict::dict_models::Article;
use rust_wheel::common::query::pagination::PaginateForQueryFragment;
use crate::model::diesel::dict::dict_schema::article::created_time;

mod model;

fn main() {
    use model::diesel::dict::dict_schema::article as article_table;
    let mut query = article_table::table.into_boxed::<diesel::pg::Pg>();
    let connection = config::establish_connection();
    let ts_query = to_tsquery("历史");
    let ts_vector = to_tsvector("'dolphin1zhcfg', title");
    query = query.filter(ts_vector.matches(ts_query));
    let query = query
        .order(created_time.desc())
        .paginate(1,false)
        .per_page(10);
    let sql1 = debug_query::<diesel::pg::Pg, _>(&query);
    println!("sql:{}",sql1);
    let query_result = query.load_and_count_pages::<Article>(&connection);

    println!("ok")

}

this is the dict_models.rs:

// Generated by diesel_ext

#![allow(unused)]
#![allow(clippy::all)]

use std::io::Write;
use diesel::deserialize::FromSql;
use diesel::pg::Pg;
use diesel::serialize::{Output, ToSql};
use diesel::sql_types::Jsonb;
use rocket::serde::Serialize;
use serde::Deserialize;
use chrono::DateTime;
use chrono::Utc;
use crate::model::diesel::dict::dict_schema::*;

#[derive(Queryable,QueryableByName,Debug,Serialize,Deserialize,Default,Clone)]
#[table_name = "article"]
pub struct Article {
    pub id: i64,
    pub user_id: i64,
    pub title: String,
    pub author: String,
    pub guid: String,
    pub created_time: i64,
    pub updated_time: i64,
    pub link: Option<String>,
    pub sub_source_id: i64,
    pub cover_image: Option<String>,
    pub channel_reputation: i32,
    pub editor_pick: Option<i32>,
}

this is the dict_scheme.rs:

table! {
    article (id) {
        id -> Int8,
        user_id -> Int8,
        title -> Varchar,
        author -> Varchar,
        guid -> Varchar,
        created_time -> Int8,
        updated_time -> Int8,
        link -> Nullable<Varchar>,
        sub_source_id -> Int8,
        cover_image -> Nullable<Varchar>,
        channel_reputation -> Int4,
        editor_pick -> Nullable<Int4>,
    }
}

and this is the project full dependencies:

[package]
name = "rust-learn"
version = "0.1.0"
edition = "2018"

[dependencies]
rocket = { version = "=0.5.0-rc.2", features = ["json"] }

serde = { version = "1.0.64", features = ["derive"] }
serde_json = "1.0.64"
serde_derive = "1.0"
# database
diesel = { version = "1.4.7", features = ["postgres","serde_json"] }
dotenv = "0.15.0"
jsonwebtoken = "7"
chrono = { version = "0.4", features = ["serde"] }
diesel_full_text_search = "1.0.1"
rust_wheel = { git = "https://github.com/jiangxiaoqiang/rust_wheel.git" }

I write the wrong sql deliberately to test the full text procedure. To my surprise, the rust diesel did not capture error information and just return 0 records. I think it should throw some error from PostgreSQL 13 shows that the sql have a bad grammer. Why did this happen? what should I do to make it works as expect? when I copy the execute sql and execute in DBeaver, it shows error. the dolphin1zhcfg did not exists. This is the article table DDL:

-- public.article definition

-- Drop table

-- DROP TABLE public.article;

CREATE TABLE public.article (
    id int8 NOT NULL GENERATED ALWAYS AS IDENTITY,
    user_id int8 NOT NULL,
    title varchar(512) NOT NULL,
    author varchar(256) NOT NULL,
    guid varchar(512) NOT NULL,
    created_time int8 NOT NULL,
    updated_time int8 NOT NULL,
    link varchar(512) NULL,
    pub_time timestamptz NULL,
    sub_source_id int8 NOT NULL,
    cover_image varchar(1024) NULL,
    channel_reputation int4 NOT NULL DEFAULT 0,
    editor_pick int4 NULL DEFAULT 0,
    permanent_store int2 NOT NULL DEFAULT 0,
    CONSTRAINT article_id_seq_pk PRIMARY KEY (id),
    CONSTRAINT article_title_sub_source_id_key UNIQUE (title, sub_source_id)
);
CREATE INDEX article_sub_source_id_idx ON public.article USING btree (sub_source_id);
CREATE INDEX article_title_gin ON public.article USING gin (to_tsvector('english'::regconfig, (title)::text));
CREATE INDEX article_title_zh_gin ON public.article USING gin (to_tsvector('dolphinzhcfg'::regconfig, (title)::text));
CREATE INDEX idx_article_created_time ON public.article USING btree (created_time);

Am I missing something?