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

Delegating values with default for generation in database. #777

Open
teckwan opened this issue Aug 3, 2022 · 7 comments
Open

Delegating values with default for generation in database. #777

teckwan opened this issue Aug 3, 2022 · 7 comments

Comments

@teckwan
Copy link

teckwan commented Aug 3, 2022

Hello! First of all I'd like to thank the maintainers, as this gem has been very useful for me and my organisation.

I have a question about the intended functionality of the gem. As context, I have a model whose id is generated by a custom function in the database. When importing using instantiated objects, the id field is added into the insert query, and hence tries to insert values with id as NULL. I am able to work around this by using an array of hashes to explicitly control the attributes which are included in the insert query.

Do you see any constraints/is it possible to expect a feature which allows us to delegate value generation for the database? (under certain restrictions where all objects in input has to have the concerned value of the field at nil for example). I'd be interested in implementing something along the lines with your approval/guidance.

Thanks for your time!

@jkowens
Copy link
Collaborator

jkowens commented Aug 3, 2022

Hi @teckwan, so there isn't much new development going on as far as adding features. There have been others who have run into the issue you are describing when using models to import into to tables that have fields that use generated values. I'd be happy to consider your ideas for solving this problem. I don't know if there is a way to dynamically identify such fields or if a parameter would need to be passed to exclude certain fields.

@teckwan
Copy link
Author

teckwan commented Aug 4, 2022

Thanks for the speedy response @jkowens.

I'm no connaisseur of the interface between ActiveRecord and the database but I don't think the ActiveRecord API gives us this information. Off the bat a possible implementation would be:

Query the database for this information at the start only when an option is given. Concretely i think it would look somewhat like this:

users = [
  User.new(name: "foo"),
  User.new(name: "bar"),
  User.new(name: "baz")
]
User.import!(users, use_defaults: [:age])

We're able to get the column default through

select column_default from information_schema.columns where table_name = 'users' and column_name = 'age';

Much like how we raise when the hashes given in input do not share the same structure, we could also raise here if the specified column(s) in use_defaults have a non-nil value.
This seems to work but introduces surely an overhead, I'm not sure if its acceptable.

Would love to know what you think about this solution.

@jkowens
Copy link
Collaborator

jkowens commented Aug 4, 2022

@teckwan that looks like a good solution to me. I think that would help resolve the issue described here #757 as well. If you want to work on building this feature out it would be welcome 👍

@teckwan
Copy link
Author

teckwan commented Aug 8, 2022

@jkowens would be a pleasure! I'll try to come up with something in the coming days.

@Amnesthesia
Copy link

@teckwan did you end up solving this?

@teckwan
Copy link
Author

teckwan commented May 25, 2023

Hi @Amnesthesia, unfortunately no. In the end I could not find time to dedicate for implementing it. If you're interested in implementing this don't hesitate!

@Amnesthesia
Copy link

@teckwan I think I did, I opened a PR you can check out

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

3 participants