Skip to content
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

mvp code for create method issue with Uuid v4 #15

Open
arkanoider opened this issue Dec 16, 2023 · 1 comment
Open

mvp code for create method issue with Uuid v4 #15

arkanoider opened this issue Dec 16, 2023 · 1 comment

Comments

@arkanoider
Copy link

Hi @treydempsey ,

we are having a weird issue with create method adding an entry to an sql table with two Uuid.

I have prepared an mvp here:

MVP issue Uuid

In the readme there is a more detailed explanation.

Thanks for your support in advance!

@arkanoider
Copy link
Author

Adding some comments about documentation...

If I try to assemble a small example based on this example it seems not working. Below here a code snippet:

use sqlx::FromRow;
use sqlx_crud::SqlxCrud;
use sqlx_crud::Crud;
use sqlx::SqlitePool;
use anyhow::Result;

#[derive(Debug, FromRow, SqlxCrud)]
pub struct User {
   pub user_id: i32,
   pub name: String,
}

#[tokio::main]
async fn main() -> Result<()> {
    let db_url = "sqlite://test.db";

    let pool = SqlitePool::connect(db_url).await?;

    let new_user = User { user_id: 1, name: "new_user".to_string() };
    new_user.create(&pool).await?;

    if let Some(user) = User::by_id(&pool, 1).await? {
        println!("User: {:?}", user);
    }
  
    Ok(())
}

In migrations folder I have this sql:

CREATE TABLE users (
    id INTEGER PRIMARY KEY NOT NULL,
    username TEXT NOT NULL
);

Basically your example in crate docs.
If I run with cargo run i get:

Error: error returned from database: (code: 1) no such column: users.user_id

Caused by:
    (code: 1) no such column: users.user_id

What did the magic for me is adding #[external_id] to the struct like that:

#[derive(Debug, FromRow, SqlxCrud)]
#[external_id]
pub struct User {
   pub user_id: i32,
   pub name: String,
}

I think some correction are needed anyway in the docs, but beside that is it ok that I use that attribute to make it work?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant