Looked at a couple other questions like this but haven't found a solution yet. Any help greatly appreciated!
I'm setting up a Rails 5 API with Postgres. I have a basic test (Minitest) that should create a valid user:
test "valid user" do
cyclist = User.new(
type: "cyclist",
first_name: "Lyla",
last_name: "Legstrong",
email: "lyla@legstrong.co",
password: "passtest123!",
bio: "I am an avid cyclist with strong, strong legs.",
avg_rating: 8
)
end
Running the test produces the following error:
Error:
UserTest#test_valid_user:
ActiveRecord::RecordNotUnique: PG::UniqueViolation:
ERROR: duplicate key value violates unique constraint "users_pkey"
DETAIL: Key (id)=(980190962) already exists.
: INSERT INTO "users" ("type", "first_name", "last_name", "photo",
"email", "password_digest", "bio", "avg_rating", "created_at",
"updated_at", "id") VALUES (NULL, 'MyString', 'MyString', 'MyString',
'MyString', 'MyString', 'MyText', 1, '2017-02-02 19:46:15.655673',
'2017-02-02 19:46:15.655673', 980190962)
I have tried the solutions suggested in the top two answers for this question, but continued getting exactly the same error. I also tried this - it prints out setval ----------- 980190967 (highest value I've seen yet so it seems like it should have worked) but when I run the test I get the same error again, with the same duplicated pkey (980190962).
One more thing: running User.create in the rails console also produces the same error. If I run it two or three times in a row, it tries to assign the next ID in the sequence (which is also taken), then the next (same issue), and then the next one after that is available and it works.
Not sure if this could be affecting things, but I'm working with a single table inheritance setup - a User superclass with two subclasses that inherit from it (ie type: "cyclist" in the test). Also, I'm fairly new to this so feel free to suggest simple things I may have missed...thanks!