when i use Datetime to insert datetime to postgres is ok,but when i use the Datetime to get the value from postgres is error:
Notification:mismatched types; Rust type chrono::datetime::DateTime<chrono::offset::utc::Utc>
(as SQL type TIMESTAMPTZ
) is not compatible with SQL type TIMESTAMP
my code:
`
#[tokio::main]
async fn main() {
let link = "postgres://postgres:bigpig@192.168.2.75:5432/test";
let opt = PgPoolOptions::new()
.max_connections(5)
.connect(link)
.await
.expect("error database");
let date_time = Utc::now();
let test_id = scru128::new().to_string();
let test1 = Test1::new(test_id,date_time);
let _result = test1.insert(&opt).await.expect("insert failed");
let query_result = Test1::get_all(&opt).await.expect("query failed");
println!("{:?}", query_result);
}
#[derive(Debug,Clone,FromRow,Serialize,Deserialize)]
pub struct Test1{
id:String,
datetime:DateTime
}
impl Test1{
pub fn new(id:String,datetime:DateTime)->Self{
Test1{
id,
datetime
}
}
pub async fn insert(self,db:&PgPool)->Result<()>{
let sql = "insert into test1(id,datetime) values($1,$2)";
sqlx::query(sql)
.bind(self.id)
.bind(self.datetime)
.execute(db)
.await?;
Ok(())
}
pub async fn get_all(db:&PgPool)->Result<Vec<Self>>{
let sql = "select * from test1";
let result = sqlx::query_as::<_,Test1>(sql)
.fetch_all(db)
.await?;
Ok(result)
}
}
t`