use ormer::Model;
mod _test_common;
#[derive(Debug, Model)]
#[table = "test_users"]
struct TestUser {
#[primary(auto)]
id: i32,
name: String,
}
#[derive(Debug, Model)]
#[table = "test_roles"]
struct TestRole {
#[primary]
id: i32,
#[foreign(TestUser)]
user_id: i32,
role_name: String,
}
async fn test_foreign_key_creation_impl(
config: &_test_common::DbConfig,
) -> Result<(), Box<dyn std::error::Error>> {
let db = _test_common::create_db_connection(config).await?;
db.create_table::<TestUser>().await?;
db.create_table::<TestRole>().await?;
println!("Tables created successfully with foreign key constraints");
db.insert(&TestUser {
id: 1,
name: "Alice".to_string(),
})
.await?;
db.insert(&TestRole {
id: 1,
user_id: 1,
role_name: "admin".to_string(),
})
.await?;
println!("Foreign key test passed!");
db.drop_table::<TestRole>().await?;
db.drop_table::<TestUser>().await?;
Ok(())
}
test_on_all_dbs_result!(test_foreign_key_creation_impl);