Skip to content

Commit

Permalink
fix relationship queries
Browse files Browse the repository at this point in the history
  • Loading branch information
m1guelpf committed Dec 15, 2023
1 parent 267f586 commit 88ab4e3
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ensemble/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ensemble"
version = "0.0.3"
version = "0.0.4"
readme = { workspace = true }
edition = { workspace = true }
authors = { workspace = true }
Expand Down
12 changes: 10 additions & 2 deletions ensemble/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,9 +572,17 @@ pub struct Columns(Vec<(String, Value)>);

impl Columns {
fn escape(column: &str) -> String {
let parts = column.split('.');

match connection::which_db() {
Database::MySQL => format!("`{column}`"),
Database::PostgreSQL => format!("\"{column}\""),
Database::MySQL => parts
.map(|part| format!("`{part}`"))
.collect::<Vec<String>>()
.join("."),
Database::PostgreSQL => parts
.map(|part| format!("\"{part}\""))
.collect::<Vec<String>>()
.join("."),
}
}
}
Expand Down
1 change: 0 additions & 1 deletion ensemble/src/types/datetime.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use fastdate::Date;
use rbs::Value;
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};
use std::{
Expand Down
2 changes: 1 addition & 1 deletion examples/migrations/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/relationships/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 27 additions & 27 deletions examples/relationships/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
use std::env;

use ensemble::{
relationships::{BelongsTo, HasMany, Relationship},
types::{DateTime, Hashed},
Model,
relationships::{BelongsTo, HasMany, Relationship},
types::{DateTime, Hashed},
Model,
};

#[derive(Debug, Model)]
pub struct User {
pub id: u64,
pub name: String,
pub email: String,
pub password: Hashed<String>,
pub created_at: DateTime,
pub updated_at: DateTime,
#[model(foreign_key = "author_id")]
pub posts: HasMany<User, Post>,
pub id: u64,
pub name: String,
pub email: String,
pub password: Hashed<String>,
pub created_at: DateTime,
pub updated_at: DateTime,
// #[model(foreign_key = "author_id")]
pub posts: HasMany<User, Post>,
}

#[derive(Debug, Model)]
pub struct Post {
#[model(incrementing)]
pub id: u64,
pub title: String,
pub content: String,
pub created_at: DateTime,
pub updated_at: DateTime,
#[model(incrementing)]
pub id: u64,
pub title: String,
pub content: String,
pub created_at: DateTime,
pub updated_at: DateTime,

#[model(foreign_key = "author_id")]
pub user: BelongsTo<Post, User>,
// #[model(foreign_key = "author_id")]
pub user: BelongsTo<Post, User>,
}

#[tokio::main]
async fn main() {
ensemble::setup(&env::var("DATABASE_URL").expect("DATABASE_URL must be set"))
.expect("Failed to set up database pool.");
ensemble::setup(&env::var("DATABASE_URL").expect("DATABASE_URL must be set"))
.expect("Failed to set up database pool.");

let mut user = User::find(1).await.expect("Failed to find user.");
let posts = user.posts.get().await.expect("Failed to get posts.");
dbg!(posts);
let mut user = User::find(1).await.expect("Failed to find user.");
let posts = user.posts.get().await.expect("Failed to get posts.");
dbg!(posts);

let mut post = Post::find(1).await.expect("Failed to find post.");
let user = post.user.get().await.expect("Failed to get user.");
dbg!(user);
let mut post = Post::find(1).await.expect("Failed to find post.");
let user = post.user.get().await.expect("Failed to get user.");
dbg!(user);
}
2 changes: 1 addition & 1 deletion examples/user/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 88ab4e3

Please sign in to comment.