Could you expand a bit more what the intention is? Are you trying to bulk-insert X509Issuers, or X509Certificates? If you want to bulk-insert the latter via the former, you need to include BulkInsertableAssociations.
@mkaeppler In this example it's the former. I chose X509Issuer as an example in this issue because it's a simple model. But my real-life case is actually DesignManagement::Design!41714 (comment 410901814). I want to bulk insert data into design_management_designs and I can with ::Gitlab::Database.bulk_insert but not by using BulkInsertSafe because of this error.
Right, I see now. Yes this indeed is not supported currently because we'd have to recursively walk the tree of relations to find out whether all of them are also BulkInsertSafe, then bulk-insert those too.
I don't think this is a bug; it is a missing feature. We should prioritize accordingly.
has_many as per the current design is only supported when the association is bulk-inserted through its owner explicitly via bulk_insert_associations! (as per the docs.)
Yes this indeed is not supported currently because we'd have to recursively walk the tree of relations to find out whether all of them are also BulkInsertSafe, then bulk-insert those too.
In the situation of Design at least, I don't care about inserting associations data at all, just the data that should go into design_management_designs. So in this case it seems like it should not matter whether the associations are considered safe or not.
Yes, I see, it's an interesting point. I need to think about it more. It sounds like in this case we should indeed allow the call, but there are other cases where not inserting dependencies might cause integrity issues.
I see from the MR you linked that you're not currently blocked on this correct? I don't think I'll get to this issue anytime soon since our team is pretty swamped with the image scaling work currently.
Thanks, @mkaeppler. I really appreciate you looking and thinking about this 🙏.
I'm not blocked and can use the deprecated ::Gitlab::Database.bulk_insert. It means that this issue would be a blocker to #221032, so I'll mark it as such.
/cc @syasonik This is a duplicate of the error you were seeing today.
For context, we have a similar situation that @.luke had. We have an existing model that had BulkInsertSafe defined and we are trying to add a new has_many association.
Like @.luke, I don't think we have plans to bulk insert data for the has_many association through it's parent.
@mkaeppler Is there any reason why we can't allow the auto-generated Rails callbacks (similar to the already allowed autosave_associated_records_for_ one) for has_many associations, provided there is no autosave: true or accepts_nested_attributes_for defined in the class?
No reason other than we didn't consider it when we wrote the first version of this, because it wasn't necessary at the time.
The main rule we should follow is that we cannot bulk insert something that itself holds references to something that isn't bulk-insertable. Even if autosave is false, doesn't that create integrity issues, because I now have records in the database that hold references to non-persisted associations? 🤔
Even if autosave is false, doesn't that create integrity issues, because I now have records in the database that hold references to non-persisted associations?
In the case of the has_many the foreign keys/references are on the associated tables, and not the table we are discussing bulk inserting into.
I imagine a typical workflow might be:
Bulk insert some models
Fire off a job for business logic + insert for data for the has_many
@minac@ifrenkel@mallocke Do you have any input on this? I've seen in multiple instances where this has caused confusion during development, or where we have chosen to deliberately bypass this validation by changing include ordering.
There are cases where it's safe to ignore these associations: Either because they are nullable, or because we are bulk inserting the association ids as well. Since BulkInsertSafe does model validation, shouldn't that be able to catch such issues? If an association is required and the foreign key for that association is not present, the model validation would fail.
I don't really know why we even check the callbacks. To me, BulkInsertSafe is just a wrapper around ActiveRecord::InsertAll with an additional feature of running the validations. If we are worried about data integrity issues, we should have already created the foreign key constraints and model layer presence validations(not for the association as it causes N+1 query issues).
For the record, as most of the discussion here is around has_many, a similar issue occurs with has_one ... :through
where active record tries to define an after_create callback. I haven't confirmed whether it occurs for has_one without :through but I think it will.
BulkInsertSafe::MethodNotAllowedError: Not allowed to call `set_callback(create, [:after, :autosave_associated_records_for_author
For has_one, and possibly other association types, :autosave is enabled implicitly:
Note that <tt>autosave: false</tt> is not same as not declaring <tt>:autosave</tt>.When the <tt>:autosave</tt> option is not present then new association records aresaved but the updated association records are not saved.
Contributions like this are vital to help make GitLab a better product.
We would be grateful for your help in verifying whether your bug report requires further attention from the team. If you think this bug still exists, and is reproducible with the latest stable version of GitLab, please comment on this issue.
This issue has been inactive for more than 12 months now and based on the policy for inactive bugs, will be closed in 7 days.
Thanks for your contributions to make GitLab better!
Contributions like this are vital to help make GitLab a better product.
We would be grateful for your help in verifying whether your bug report requires further attention from the team. If you think this bug still exists, and is reproducible with the latest stable version of GitLab, please comment on this issue.
This issue has been inactive for more than 12 months now and based on the policy for inactive bugs, will be closed in 7 days.
Thanks for your contributions to make GitLab better!
I came across this issue in !145881 (comment 1798741796). As many have already noted, the current workaround is to put include BulkInsertSafe after the has_many declaration. We see this enacted in multiple places in the codebase: