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

Dealing with MySQL VARCHAR maximum length #1516

Closed
connorworley opened this Issue Jan 30, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@connorworley

connorworley commented Jan 30, 2018

Right now, it looks like the Varchar type simply aliases to Text. On MySQL databases, this is bound to cause problems for a VARCHAR column with max length specified. For example, the following test:

#[macro_use]
extern crate diesel;

#[cfg(test)]
mod tests {
    use diesel;
    use diesel::prelude::*;
    use diesel::mysql::MysqlConnection;

    mod schema {
        table! {
            test_users {
                id -> Integer,
                name -> Varchar,
            }
        }
    }

    mod models {
        use super::schema::test_users;

        #[derive(Queryable)]
        pub struct User {
            pub id: i32,
            pub name: String,
        }

        #[derive(Insertable)]
        #[table_name="test_users"]
        pub struct NewUser<'a> {
            pub id: i32,
            pub name: &'a str,
        }
    }

    #[test]
    fn test_mysql_varchar_length() {
        let database_url = "mysql://test:test@localhost/test";
        let connection = MysqlConnection::establish(database_url).unwrap();

        // drop test table if it exists already
        diesel::sql_query(
            "DROP TABLE test_users;"
            )
            .execute(&connection)
            .unwrap_or_default();

        // create test table
        diesel::sql_query(
            "CREATE TABLE test_users ( \
                id INT PRIMARY KEY, \
                name VARCHAR(20) \
            );"
            )
            .execute(&connection)
            .expect("Failed to run setup SQL");

        // create test user 1
        {
            use self::schema::test_users;
            use self::models::*;

            let new_user = NewUser {
                id: 1,
                name: &"Under 20 characters".to_string(),
            };

            diesel::insert_into(test_users::table)
                .values(&new_user)
                .execute(&connection)
                .expect("Failed to insert test user");
        }

        let test_user = {
            use self::schema::test_users::dsl::*;
            use self::models::*;
            test_users.filter(id.eq(1))
                .first::<User>(&connection)
                .expect("Failed to load test user")
        };

        assert_eq!("Under 20 characters", test_user.name);

        // create test user 2
        {
            use self::schema::test_users;
            use self::models::*;

            let new_user = NewUser {
                id: 2,
                name: &"This username probably has more than 20 characters".to_string(),
            };

            diesel::insert_into(test_users::table)
                .values(&new_user)
                .execute(&connection)
                .expect("Failed to insert test user 2");
        }

        let test_user2 = {
            use self::schema::test_users::dsl::*;
            use self::models::*;
            test_users.filter(id.eq(2))
                .first::<User>(&connection)
                .expect("Failed to load test user 2")
        };

        assert_eq!("This username probably has more than 20 characters", test_user2.name);
    }
}

predictably fails:

---- tests::test_mysql_varchar_length stdout ----
	thread 'tests::test_mysql_varchar_length' panicked at 'assertion failed: `(left == right)`
  left: `"This username probably has more than 20 characters"`,
 right: `"This username probab"`', src/lib.rs:108:9

Are there any plans to keep track of Varchar length in the future? Would there be interest in a PR for this, perhaps mapping to arrayvec's fixed-capacity string type? Thanks!

@connorworley connorworley changed the title from Dealing with.MySQL VARCHAR maximum length to Dealing with MySQL VARCHAR maximum length Jan 30, 2018

@sgrif

This comment has been minimized.

Member

sgrif commented Jan 30, 2018

Are there any plans to keep track of Varchar length in the future?

Nope, we also don't currently plan on tracking any other constraints, defaults, indexes, etc.

Would there be interest in a PR for this

Not at this time. Thank you!

@sgrif sgrif closed this Jan 30, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment