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

Model#valid? Returning True For Invalid Records After Import #824

Open
Numie opened this issue Dec 14, 2023 · 7 comments
Open

Model#valid? Returning True For Invalid Records After Import #824

Numie opened this issue Dec 14, 2023 · 7 comments

Comments

@Numie
Copy link

Numie commented Dec 14, 2023

When I attempt to import a record that is invalid due to a uniqueness constraint, a number of ActiveModel methods are returning unexpected results. Specifically, valid? returns true after the import despite the record being invalid.

For example, I have a Post model with name and company_id attributes and a uniqueness validation like validates :name, uniqueness: { scope: :company_id }.

post = Post.first.dup
=>
#<Post:0x00007f6780ea8348
 id: nil,
 name: "Name",
 company_id: 1,
 created_at: nil,
 updated_at: nil>

post.valid?
=> false

post.new_record?
=> true

Then if I run an import, the return values change:

options = { validate: true, validate_uniqueness: true, recursive: true, all_or_none: true, track_validation_failures: true }
Post.import([post], options)
=> 
#<struct ActiveRecord::Import::Result
 failed_instances=
  [[0,
    #<Post:0x00007f6780ea8348
     id: nil,
     name: "Name",
     company_id: 1,
     created_at: nil,
     updated_at: nil>]],
 num_inserts=0,
 ids=[],
 results=[]>

post.valid?
=> true

post.new_record?
=> false

Post.new(post.attributes).valid?
=> false

I'd expect the return values of valid? and new_record? to remain the same after the import.

I'm using Rails 7.1 with Postgres.

@veeKewl
Copy link

veeKewl commented Jan 16, 2024

Running in to the same problem. Using Rails 5 did you figure out a reason or fix for this ?

@Numie
Copy link
Author

Numie commented Jan 17, 2024

Running in to the same problem. Using Rails 5 did you figure out a reason or fix for this ?

No, awaiting any feedback

@jkowens
Copy link
Collaborator

jkowens commented Jan 17, 2024

It seems post is being hydrated from a database record. Can you determine which record it is loading from the database by inspecting the id? I wonder if the recursive option is mistakenly populating the object from the database.

@Numie
Copy link
Author

Numie commented Jan 17, 2024

It seems post is being hydrated from a database record. Can you determine which record it is loading from the database by inspecting the id? I wonder if the recursive option is mistakenly populating the object from the database.

post.id
=> nil

So it doesn't seem like a different record is being pulled from the db. Somehow valid? is being overwritten to make a non-persisted and invalid record return true?

@jkowens
Copy link
Collaborator

jkowens commented Jan 17, 2024

Strange, I wonder why new_record? is returning false.

@veeKewl
Copy link

veeKewl commented Jan 18, 2024

have you tried adding raise_error ?? i tried it and the it showed the error for me.
on my case the company is new and not saved yet, the post.valid? was returning true for me because it was actually valid if i just use post.save! it will also save the company first then save the post.

@Numie
Copy link
Author

Numie commented Jan 23, 2024

No, I don't want to raise errors, otherwise import! could be used. I just want methods like valid? to return false correctly if the import is unsuccessful.

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