Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upCannot filter table using like() method #975
Comments
This comment has been minimized.
|
Hi, thanks for detailed issue!
I just reproduced this locally and your email field is nullable, i.e., it is inferred to be a `Nullable<Varchar>`. There is no `like` method defined for that type.
Not sure if that is a SQLite thing or if the impl is just missing from Diesel, though.
… Am 29.06.2017 um 12:51 schrieb Luke Chen Shui ***@***.***>:
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.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
This comment has been minimized.
chenshuiluke
commented
Jun 29, 2017
|
Thanks! I can't believe I spent hours trying to figure out why it wasn't working when the fix was so simple. |
killercup
added
the
question
label
Jun 29, 2017
This comment has been minimized.
|
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. |
killercup
closed this
Jun 29, 2017
This comment has been minimized.
chenshuiluke
commented
Jun 29, 2017
|
Yeah, just adding NOT NULL to the attributes in the up.sql file fixed it for me |
This comment has been minimized.
|
We should have an impl for nullable columns. |
sgrif
reopened this
Jul 4, 2017
Eijebong
added
accepted
enhancement
and removed
question
labels
Aug 14, 2017
added a commit
that referenced
this issue
Sep 26, 2017
sgrif
closed this
in
c942b1c
Sep 28, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
chenshuiluke commentedJun 29, 2017
Setup
Versions
Feature Flags
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?
Steps to reproduce
up.sql:
models.rs:
database.rs:
main.rs:
schema.rs:
Checklist