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

Cannot seed tables without autoincrement ID field #1038

Open
Swoorup opened this issue Nov 29, 2024 · 0 comments · May be fixed by #1043
Open

Cannot seed tables without autoincrement ID field #1038

Swoorup opened this issue Nov 29, 2024 · 0 comments · May be fixed by #1043

Comments

@Swoorup
Copy link

Swoorup commented Nov 29, 2024

Description

It appears that we cannot create a table without a Id column. Even if created the Id column has to be a autoincrement type.

To Reproduce

  1. Generate any model like.
cargo loco generate model post name:string! url:string! icon:string!
  1. Proceed to change the id type as either string or remove it.
  2. Add seed data and run the seed task like cargo run task seed_data refresh:true
  3. Observe the error.
Error: DB(Exec(SqlxError(Database(PgDatabaseError { severity: Error, code: "42703", message: "column \"id\" does not exist", detail: None, hint: None, position: Some(Original(70)), where: None, schema: None, table: None, column: None, data_type: None, constraint: None, file: Some("parse_relation.c"), line: Some(3656), routine: Some("errorMissingColumn") }))))

In the db logs, we see:

2024-11-28 19:38:29.131 UTC [375] STATEMENT:  SELECT setval(pg_get_serial_sequence('post', 'id'), COALESCE(MAX(id), 0) + 1, false) FROM post

Expected Behavior

Should run without any issues

Environment:

MacOSX

Additional Context

The issue is likely coming from this piece of logic here:

loco/src/db.rs

Lines 282 to 319 in e1b0392

pub async fn reset_autoincrement(
db_backend: DatabaseBackend,
table_name: &str,
db: &DatabaseConnection,
) -> crate::Result<()> {
match db_backend {
DatabaseBackend::Postgres => {
let query_str = format!(
"SELECT setval(pg_get_serial_sequence('{table_name}', 'id'), COALESCE(MAX(id), 0) \
+ 1, false) FROM {table_name}"
);
db.execute(Statement::from_sql_and_values(
DatabaseBackend::Postgres,
&query_str,
vec![],
))
.await?;
}
DatabaseBackend::Sqlite => {
let query_str = format!(
"UPDATE sqlite_sequence SET seq = (SELECT MAX(id) FROM {table_name}) WHERE name = \
'{table_name}'"
);
db.execute(Statement::from_sql_and_values(
DatabaseBackend::Sqlite,
&query_str,
vec![],
))
.await?;
}
DatabaseBackend::MySql => {
return Err(Error::Message(
"Unsupported database backend: MySQL".to_string(),
))
}
}
Ok(())
}

Should this logic change?

@Swoorup Swoorup linked a pull request Dec 1, 2024 that will close this issue
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

Successfully merging a pull request may close this issue.

1 participant