-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Labels
Comments
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.
|
Thanks! I can't believe I spent hours trying to figure out why it wasn't working when the fix was so simple. |
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. |
Yeah, just adding NOT NULL to the attributes in the up.sql file fixed it for me |
We should have an impl for nullable columns. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
The text was updated successfully, but these errors were encountered: