django-rest-passwordreset icon indicating copy to clipboard operation
django-rest-passwordreset copied to clipboard

IntegrityError at /api/password_reset/ null value in column "ip_address" violates not-null constraint

Open TorfinS opened this issue 6 years ago • 14 comments

Hi, I have a problem with this dependency on production server. Locally works very good, but problem appears on server. It Django issue is?

django-rest-passwordreset version: 0.9.7 Django: 1.11.5

TorfinS avatar Apr 04 '19 17:04 TorfinS

If you need any logs, let me know what you need.

TorfinS avatar Apr 04 '19 21:04 TorfinS

Hi,

it might be possible that your request does not have "ip address" set. You are probably using a reverse proxy or load balancer or a unix socket?

Take a look at this PR and the comments, the HTTPXForwardedForMiddleware might help you: https://github.com/anx-ckreuzberger/django-rest-passwordreset/pull/7

anx-ckreuzberger avatar Apr 05 '19 07:04 anx-ckreuzberger

This should be fixed by PR #40. It's not yet in a release, but it's in the master branch.

anx-ckreuzberger avatar May 10 '19 06:05 anx-ckreuzberger

Should be fixed by release 1.1.0rc1, you can install it using

pip install "django-rest-passwordreset==1.1.0rc1"

anx-ckreuzberger avatar May 28 '19 06:05 anx-ckreuzberger

@anx-ckreuzberger cant get it to work 😞

joaquinperaza avatar Jun 05 '19 15:06 joaquinperaza

Can you provide an error message/exception/...?

anx-ckreuzberger avatar Jun 18 '19 15:06 anx-ckreuzberger

pip install "django-rest-passwordreset==1.1.0rc1"

in my case it does not work, because the:

request.META.get('REMOTE_ADDR', getattr(settings, 'DJANGO_REST_PASSWORDRESET_REMOTE_ADDR', ''))

returns empty string, not null add a validation and it worked :(

solve it like this:

ip_address = request.META.get('REMOTE_ADDR', getattr(settings, 'DJANGO_REST_PASSWORDRESET_REMOTE_ADDR', ''))
user_agent = request.META.get('HTTP_USER_AGENT', getattr(settings, 'DJANGO_REST_PASSWORDRESET_HTTP_USER_AGENT', ''))
token = ResetPasswordToken.objects.create(
    user = user,
    user_agent = getattr(settings, 'DJANGO_REST_PASSWORDRESET_HTTP_USER_AGENT', '') if user_agent == '' else user_agent,
    ip_address = getattr(settings, 'DJANGO_REST_PASSWORDRESET_REMOTE_ADDR', '') if ip_address == '' else ip_address
)

idkc-aios avatar Jul 09 '19 19:07 idkc-aios

@anx-ckreuzberger I've also bumped to the pre-release version 1.1.0rc1, but the IntegrityError still remains, even when I've set DJANGO_REST_PASSWORDRESET_HTTP_USER_AGENT and DJANGO_REST_PASSWORDRESET_REMOTE_ADDR in my settings configuration.

This is pretty weird because I've checked them about a hundred times but it still tries to set a null value for ip_address, which raises the error.

Any ideas? :)

wencakisa avatar Jul 10 '19 08:07 wencakisa

look for the file in your local project venv/lib... modify the file in the line that I put

https://github.com/anx-ckreuzberger/django-rest-passwordreset/blob/85d30b7129605cf7651a79ebabb3dbac5e01e181/django_rest_passwordreset/views.py#L140

modify as I put in my previous comment

idkc-aios avatar Jul 10 '19 19:07 idkc-aios

I know I'm the one that pressed the merge button on PR #40 , but I guess

                    # no token exists, generate a new token
                    token = ResetPasswordToken.objects.create(
                        user=user,
                        user_agent=request.META.get('HTTP_USER_AGENT',
                                                    getattr(settings, 'DJANGO_REST_PASSWORDRESET_HTTP_USER_AGENT', '')),
                        ip_address=request.META.get('REMOTE_ADDR',
                                                    getattr(settings, 'DJANGO_REST_PASSWORDRESET_REMOTE_ADDR', ''))
                    )

is just wrong?

I believe the implementation suggested by @vonorm here https://github.com/anx-ckreuzberger/django-rest-passwordreset/pull/40#issuecomment-506752772 is correct.

Any thoughts?

anx-ckreuzberger avatar Jul 24 '19 11:07 anx-ckreuzberger

The idea is to work and not return error.

The problem that I had was that this request.META.get ('REMOTE_ADDR') returned empty text and therefore never set the default value assigned from the setting.

that say this: request.META.get ('REMOTE_ADDR',                                                      getattr (settings, 'DJANGO_REST_PASSWORDRESET_REMOTE_ADDR', '')) returned empty text, which does not allow it to be inserted into the database.

There are several alternatives, I suggested what worked for me PD: thanks

idkc-aios avatar Jul 25 '19 20:07 idkc-aios

The bugfixes have been released on PyPi in version 1.1.0rc3.

You should be able install and test it using

pip install django-rest-passwordreset==1.1.0rc3

anx-ckreuzberger avatar Aug 09 '19 06:08 anx-ckreuzberger

I am still getting the error: null value in column "ip_address" violates not-null constraint even after installing django-rest-passwordreset==1.1.0rc3 :(

Worked for me after migrating 👯‍♂

shiveshsky avatar Aug 20 '19 16:08 shiveshsky

There is another error Data too long for column 'ip_address'. This happens with the header

X-Forwarded-For | 2605:e000:100b:81b3:a5bb:913e:80c0:6e4, 172.68.47.143

DJANGO_REST_PASSWORDRESET_IP_ADDRESS_HEADER is set to use HTTP_X_FORWARDED_FOR. Do I have to use HTTP_X_REAL_IP?

discover59 avatar Mar 03 '20 07:03 discover59