79230991

Date: 2024-11-27 16:03:45
Score: 3.5
Natty:
Report link

Can you please try, if

use serde::{Deserialize, Serialize};
use surrealdb::engine::any::Any;
use surrealdb::{engine::any::connect, Surreal};
use surrealdb::{Error, RecordId};

#[derive(Debug, Serialize, Deserialize)]
pub struct Product {
    pub name: String,
    pub qty: i64,
    pub price: f64,
    pub category: RecordId,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Category {
    pub name: String,
    pub id: RecordId,
}

pub async fn create_product(db: &Surreal<Any>) -> Result<(), Error> {
    let category: Option<Category> = db
        .query("CREATE category SET name = 'Equity'")
        .await?
        .take(0)?;

    let product: Option<Product> = db
        .create("product")
        .content(Product {
            name: "Banana".to_string(),
            qty: 100,
            price: 5.45,
            category: category.unwrap().id,
        })
        .await?;

    println!("Product created: {product:?}");
    Ok(())
}

#[tokio::main]
async fn main() -> surrealdb::Result<()> {
    let db = connect("mem://").await?;
    db.use_ns("ns").use_db("db").await?;

    db.query(
        "DEFINE TABLE category SCHEMALESS;
DEFINE FIELD name ON category TYPE string;
DEFINE TABLE product SCHEMALESS;
DEFINE FIELD name ON product TYPE string;
DEFINE FIELD price ON product TYPE float;
DEFINE FIELD qty ON product TYPE int;
DEFINE FIELD category ON product TYPE record <category>;",
    )
    .await?;

    create_product(&db).await?;
    Ok(())
}

solves the issue?

Reasons:
  • RegEx Blacklisted phrase (1.5): solves the issue?
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Starts with a question (0.5): Can you please
  • Low reputation (1):
Posted by: TheUnknown