TimestamptzSqlite in SQLite schema breaks diesel migration (or gets overwritten) #4192
-
I've been trying to set up Diesel with the SQLite backend, and using chrono's The context
But
and it would be saved internally as a text field, but it was decided not to implement it in this case. @weiznich's suggested how to handle this in #3320 (comment):
My setupAll the below code is using the following [package]
name = "diesel-sqlite"
version = "0.1.0"
edition = "2021"
[dependencies]
diesel = { version = "2.2.2", features = ["sqlite", "chrono"] } Option one: write the SQL, then edit the schemaFollowing the Diesel tutorial, I first handwrote my SQL: migrations/2024-xxxx-initial/up.sql -- Your SQL goes here
CREATE TABLE `things`(
`id` INTEGER PRIMARY KEY NOT NULL,
`name` TEXT NOT NULL,
`last_updated_at` TIMESTAMP NOT NULL
); I did src/schema.rs // @generated automatically by Diesel CLI.
diesel::table! {
things (id) {
id -> Integer,
name -> Text,
// Was Timestamp; edited by me to TimestamptzSqlite
last_updated_at -> TimestamptzSqlite,
}
} This works fine to start with. But when I run any subsequent (Maybe I should disable auto-updating schema.rs if I'm manually editing it? But it was auto-generated the first time, so it seems wrong to be manually writing SQL the first time but editing the schema on subsequent rounds....) Option 2: Write the schema, then generate the SQLThe other option is to manually make a diesel::table! {
things (id) {
id -> Integer,
name -> Text,
last_updated_at -> TimestamptzSqlite,
}
} Running -- Your SQL goes here
CREATE TABLE `things`(
`id` INTEGER NOT NULL PRIMARY KEY,
`name` TEXT NOT NULL,
`last_updated_at` TIMESTAMPTZSQLITE NOT NULL
); ...so Diesel decided to define its own custom field after all! Unfortunately, runnig
The only way to get it to run is:
Under the default config, this overwrites After the migration is run, I can edit My questions
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
That's on of the cases where you are currently expected to manually change the generated SQL. We explicitly do not apply these migrations automatically for exactly this reason. As for updating the
We are always open to contributors that improve the documentation of something, but be warned that this case is a bit special and we generally do not like to introduce special handing for types anywhere outside of the type itself. That explicitly includes generating the schema.rs file and generating the migrations.
It's currently expected behavior as we don't perform any type checking there. We literally always take the name of the type as written in your schema.rs file as we don't want to have any special casing there. So to sum that up:
That all written: Obviously things could be better here. The proper solution would be to give you as a user more influence on generating these types names (in your schema.rs and in the generated migrations). That would require some extension of the configuration file ( |
Beta Was this translation helpful? Give feedback.
That's on of the cases where you are currently expected to manually change the generated SQL. We explicitly do not apply these migrations automatically for exactly this reason. As for updating the
schema.rs
file: The recommend way for this is to use a patch file as described in the corresponding guide (See thepatch_file
section there for details).