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

Support for GENERATED ALWAYS AS IDENTITY columns #30

Open
mattfbacon opened this issue Apr 15, 2022 · 3 comments
Open

Support for GENERATED ALWAYS AS IDENTITY columns #30

mattfbacon opened this issue Apr 15, 2022 · 3 comments
Labels
documentation Improvements or additions to documentation

Comments

@mattfbacon
Copy link

As far as I know, it is generally considered best practice to enforce that the ID column will always be generated. The GENERATED ALWAYS modifier allows this. Additionally, the AS IDENTITY modifier creates the numeric sequence.

The documentation for deriving Table states that:

ormx will generate a helper struct for inserting rows into the database when using #[ormx(insertable)].
This struct will contain all fields of the struct, except

  • the ID
  • fields annotated with #[ormx(default)]

since the value of these fields will be generated by the database.

However, the implementation of the derive macro for the Insertable trait does not follow this:

let insert_fields: Vec<&TableField<PgBackend>> = table.insertable_fields().collect();

pub fn insertable_fields(&self) -> impl Iterator<Item = &TableField<B>> + Clone {
self.fields.iter().filter(|field| !field.default)
}

I believe insertable_fields should also exclude the ID, or maybe the ID should be marked as having a default value by setting the default field to true.

@mattfbacon
Copy link
Author

mattfbacon commented Apr 15, 2022

For the moment I have worked around this by manually adding #[ormx(default)] to the field for the ID column.

@NyxCode
Copy link
Owner

NyxCode commented Apr 29, 2022

@mattfbacon thanks for opening this issue.
It seems like the documentation is out-of-date. Nowadays, the ID column is not handled specially - if it's database-generated, it has to be #[ormx(default)].

@NyxCode NyxCode added the documentation Improvements or additions to documentation label Apr 29, 2022
@mattfbacon
Copy link
Author

OK. This is still semi-relevant though because a GENERATED ALWAYS column can't be updated either, which is still allowed for fields annotated with #[ormx(default)]. In my fork, I added a #[ormx(default = "always")] option to accomplish this.

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

No branches or pull requests

2 participants