Skip to content

feat: Omit Columns from SQL statements (e.g uuid columns) - #802

Open
Amnesthesia wants to merge 3 commits into
zdennis:masterfrom
Amnesthesia:feat/column-omission
Open

feat: Omit Columns from SQL statements (e.g uuid columns)#802
Amnesthesia wants to merge 3 commits into
zdennis:masterfrom
Amnesthesia:feat/column-omission

Conversation

@Amnesthesia

Copy link
Copy Markdown

I've added two options for omitting columns from the SQL statements, to avoid having to modify class level self.ignored_columns since we've had issues with this affecting other code that runs at the same time. These options work as follows:

Omit the guid column from SQL statement, allowing it to generate the uuid and not be forced to NULL:
Model.import(values, omit_columns: [:guid])

Allow per-model decisions, e.g for recursive imports:
Model.import(values, omit_columns: -> (model, column_name) { [:guid] if model == Model })

Use a hash instead of a proc:
Model.import(values, omit_columns: { Model => [:guid] })

The second option is :omit_columns_with_default_functions boolean, to automatically find columns that have a default function declared in the schema, and omit them by default.

@Amnesthesia

Copy link
Copy Markdown
Author

Can this be merged or is this already implemented upstream? We're currently pointing our Gemfile to this branch and can't update until uuid primary keys are supported

@jacob-carlborg-apoex

Copy link
Copy Markdown

@Amnesthesia UUID primary keys already work, if they're following the standard naming convention, i.e. id. I'm using that on several tables in one of my applications. But for other columns it doesn't work.

@jacob-carlborg-apoex

Copy link
Copy Markdown

@Amnesthesia BTW, it's already possible to specify which columns should be imported. Pass an array of symbols as the first argument to import. Second code example: https://github.com/zdennis/activerecord-import?tab=readme-ov-file#activerecord-models.

@Amnesthesia

Amnesthesia commented Jan 9, 2025

Copy link
Copy Markdown
Author

@jacob-carlborg-apoex This is for auto-generated UUID columns that are not primary keys :)

I've added two options for omitting columns from the SQL statements, to avoid having to modify class level `self.ignored_columns` since we've had issues with this affecting other code that runs at the same time. These options work as follows:

`Model.import(values, omit_columns: [:guid])` # Omit the guid column from SQL statement, allowing it to generate
`Model.import(values, omit_columns: -> (model, column_name) { [:guid] if model == Model })` Allow per-model decisions, e.g for recursive imports
`Model.import(values, omit_columns: { Model => [:guid] })` Use a hash instead of a proc

The second option is `:omit_columns_with_default_functions` boolean, to automatically find columns that have a default function declared in the schema, and omit them by default.

fix: Require AR 5.0+
@Amnesthesia
Amnesthesia force-pushed the feat/column-omission branch from b19a2ed to 1226af2 Compare August 22, 2025 01:30
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 this pull request may close these issues.

2 participants