claytondaley / drf-writable-nested

Writable nested model serializer for Django REST Framework
Other
1 stars 2 forks source link

model field with unique=True causes an exception #1

Closed dfrankow closed 5 years ago

dfrankow commented 5 years ago

Run the unit tests of https://github.com/dfrankow/drf-writable-example/commit/2832ce8df6427cb6b94682d12243bccb39047b2e

I think the problem is unique=True on User.username.

I attach the error.

error.txt

claytondaley commented 5 years ago

OK I had a really weird issue... a loop used the variable name, but the loop had the string "name" hard coded which must have (coincidentally) been the unique in my internal usage. Fixed in master. Looking into your second test case.

claytondaley commented 5 years ago

The second test still fails, but due to an erroneous assertion. You're using GetOrCreate on the AccessSerializer. Both are requesting the same key so both get it. I assume you really want a CreateOnlyNestedSerializer for that case.

claytondaley commented 5 years ago

I went ahead and added a CreateOnlyNestedSerializerMixin in master. I dropped it into your example and get a different (but expected) error because the two requests use the same key. This causes a unique key collision.

claytondaley commented 5 years ago

... just to make sure the code was working, when I also drop the unique constraint on AccessKey.key, the test passes.

dfrankow commented 5 years ago

Yep, I think this one is fixed.