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

Cannot filter table using like() method #975

Closed
chenshuiluke opened this issue Jun 29, 2017 · 5 comments
Closed

Cannot filter table using like() method #975

chenshuiluke opened this issue Jun 29, 2017 · 5 comments

Comments

@chenshuiluke
Copy link

Setup

Versions

  • Rust:1.20.0-nightly
  • Diesel:0.13.0
  • Database:sqlite
  • Operating System: Ubuntu x86_64 GNU/Linux

Feature Flags

  • diesel:sqlite
  • diesel_codegen:sqlite

Problem Description

Basically, it seems my program won't compile because the .like() method cannot be found on one of my table columns

What are you trying to accomplish?

To delete a user from my sqlite database using his email.

What is the expected output?

The program should compile

What is the actual output?

error[E0599]: no method named `like` found for type `schema::__diesel_infer_schema::infer_users::users::columns::email` in the current scope
--> src/database.rs:41:39
|
41 |     diesel::delete(users.filter(email.like(pattern)))
|                                       ^^^^
|
= note: the method `like` exists but the following trait bounds were not satisfied:
        `&mut schema::__diesel_infer_schema::infer_users::users::columns::email : diesel::Expression`
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `like`, perhaps you need to implement it:
        candidate #1: `diesel::TextExpressionMethods`

error: aborting due to previous error(s)

Steps to reproduce

up.sql:

-- Your SQL goes here
CREATE TABLE users (
	id INTEGER PRIMARY KEY,
	name VARCHAR,
	email VARCHAR UNIQUE,
	password VARCHAR
)

models.rs:

use super::schema::users;
#[derive(Queryable)]
pub struct User {
    pub id: i32,
    pub name: String,
    pub email: String,
    pub password: String,
}

#[derive(Queryable, Insertable, Debug, Associations)]
#[table_name = "users"]
pub struct NewUser<'a> {
    pub name: &'a str,
    pub email: &'a str,
    pub password: &'a str
}

database.rs:

extern crate diesel;
use diesel::sqlite::SqliteConnection;
use diesel::prelude::*;
use models::NewUser;
use std::error::Error;
use super::*;
pub fn establish_connection() -> SqliteConnection {

    let database_url = "potara.db";
    SqliteConnection::establish(&database_url)
        .expect(&format!("Error connecting to {}", database_url))
}

pub fn create_user(name: &str, email: &str, password:&str) -> Option<usize> {
    use schema::users;

    let connection = establish_connection();

    let new_user = NewUser {
        name: name,
        email: email,
        password: password
    };

    match diesel::insert(&new_user)
        .into(users::table)
        .execute(&connection){
            Ok(num) => {
                Some(num)
            }
            Err(error) => {
                None
            }
        }
}

pub fn delete_user(email_target:String) -> usize{
    use schema::users::dsl::*;
    let connection = establish_connection();
    let pattern = format!("%{}%", email_target);
    diesel::delete(users.filter(email.like(pattern)))
        .execute(&connection)
        .expect("lol")
    // match 
    //     .execute(&connection){
    //     Ok(num) => {
    //         Some(num)
    //     }
    //     Err(error) => {
    //         None
    //     }
    // }   
}

main.rs:

#![feature(plugin)]
#![plugin(rocket_codegen)]
#![plugin(maud_macros)]
#[macro_use]

extern crate serde_derive;
extern crate toml;
extern crate rocket;
extern crate mime_guess;
extern crate maud;
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_codegen;
extern crate dotenv;

mod schema;
mod models;

mod utilities;
mod pipeline_config;
mod assets;
mod template;
mod router;
mod database;

fn main() {
    router::init();
}

schema.rs:

infer_schema!("dotenv:DATABASE_URL");

Checklist

  • [*] I have already looked over the issue tracker for similar issues.
@killercup
Copy link
Member

killercup commented Jun 29, 2017 via email

@chenshuiluke
Copy link
Author

Thanks! I can't believe I spent hours trying to figure out why it wasn't working when the fix was so simple.

@killercup
Copy link
Member

Cool! So, you were able to make the column NOT NULL?

We might want to have a nicer/documented solution for that, but we can create a new issue for that.

@chenshuiluke
Copy link
Author

Yeah, just adding NOT NULL to the attributes in the up.sql file fixed it for me

@sgrif
Copy link
Member

sgrif commented Jul 4, 2017

We should have an impl for nullable columns.

@sgrif sgrif reopened this Jul 4, 2017
sgrif added a commit that referenced this issue Sep 26, 2017
The most difficult part here is getting `Concat` to have its return type
be based on the type of its arguments. Since there's no easy way for me
to carry any possible `ty` value as a series of `tt` without accepting a
subset of possible `ty` values, I've opted only to handle the magic
`ReturnBasedOnArgs` value in the one place we need it.

Fixes #975
@sgrif sgrif closed this as completed in c942b1c Sep 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants