Unable to import recursive
joshfester opened this issue · 2 comments
Want to start by thanking the author and contributors. This gem has saved me so much time in many projects.
Here's the test I've setup:
def test_active_record_import
sku = ProductSku.new(
price: 10,
)
sku.build_retailer(
city: "Mordor",
state: "Middle Earth",
title: "Sauron's Good Good"
)
sku.build_product_variant(weight: 1)
sku.product_variant.build_product(
title: "The Balrog"
)
sku.product_variant.product.build_product_brand(title: "Mordor's Maddest")
sku.product_variant.product.build_product_type(title: "Potato")
assert_difference -> { ProductBrand.count } => 1,
-> { ProductType.count } => 1,
-> { Product.count } => 1,
-> { ProductVariant.count } => 1,
-> { ProductSku.count } => 1,
-> { Retailer.count } => 1 do
ProductSku.import [sku], validate: false, timestamps: true, recursive: true
end
end
I've tried many variations of this, but always end up with this error:
ActiveRecord::NotNullViolation: PG::NotNullViolation: ERROR: null value in column "retailer_id" of relation "product_skus" violates not-null constraint
If I run sku.save!
rather than import, it saves all of the data successfully. I've also tried other directions by importing the ProductBrand or the Product, but get similar errors for foreign keys.
Am I doing something wrong? Thanks!
Hi @joshfester. From what I remember, the "recursive" import is only able to traverse down has_many/has_one relationships. It can't handle belongs_to. In your example, I think you may need to do separate imports for retailers and product brands. Then import product types that has_many products that has_many product variants that has_one product sku. Or something along those lines. The trick would be to associate a product sku with an already imported retailer and a product with an already imported product brand.
@jkowens Ah ok that makes sense. I should be able to make that work. Thank you!