Re: FOREIGN KEY constraint failed

2018-04-07 Thread Christophe Pettus

> On Apr 7, 2018, at 12:27, DIlIMBEK TURANOV  wrote:
> File "/home/den/HTML_Projects/HTML5/test_project/orders/views.py" in checkout
>   59. order = Order.objects.create(user=user, customer_name=name, 
> customer_phone=phone, status_id=1)

You're most likely creating an Order object that has a foreign key, but that 
foreign key isn't properly set.

--
-- Christophe Pettus
   x...@thebuild.com

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/EC168E4D-4BA0-4A20-BDF6-01333BB96C5B%40thebuild.com.
For more options, visit https://groups.google.com/d/optout.


FOREIGN KEY constraint failed

2018-04-07 Thread DIlIMBEK TURANOV


Environment:Request Method: POSTRequest URL: 
http://127.0.0.1:8000/checkout/Django Version: 2.0.3Python Version: 
3.5.2Installed Applications:['django.contrib.admin', 'django.contrib.auth', 
'django.contrib.contenttypes', 'django.contrib.sessions', 
'django.contrib.messages', 'django.contrib.staticfiles', 'landing', 'products', 
'orders']Installed Middleware:['django.middleware.security.SecurityMiddleware', 
'django.contrib.sessions.middleware.SessionMiddleware', 
'django.middleware.common.CommonMiddleware', 
'django.middleware.csrf.CsrfViewMiddleware', 
'django.contrib.auth.middleware.AuthenticationMiddleware', 
'django.contrib.messages.middleware.MessageMiddleware', 
'django.middleware.clickjacking.XFrameOptionsMiddleware']Traceback:File 
"/home/den/HTML_Projects/HTML5/test_project/venv/lib/python3.5/site-packages/django/db/backends/base/base.py"
 in _commit  239. return self.connection.commit()The above 
exception (FOREIGN KEY constraint failed) was the direct cause of the following 
exception:File 
"/home/den/HTML_Projects/HTML5/test_project/venv/lib/python3.5/site-packages/django/core/handlers/exception.py"
 in inner  35. response = get_response(request)File 
"/home/den/HTML_Projects/HTML5/test_project/venv/lib/python3.5/site-packages/django/core/handlers/base.py"
 in _get_response  128. response = 
self.process_exception_by_middleware(e, request)File 
"/home/den/HTML_Projects/HTML5/test_project/venv/lib/python3.5/site-packages/django/core/handlers/base.py"
 in _get_response  126. response = wrapped_callback(request, 
*callback_args, **callback_kwargs)File 
"/home/den/HTML_Projects/HTML5/test_project/orders/views.py" in checkout  59.   
  order = Order.objects.create(user=user, customer_name=name, 
customer_phone=phone, status_id=1)File 
"/home/den/HTML_Projects/HTML5/test_project/venv/lib/python3.5/site-packages/django/db/models/manager.py"
 in manager_method  82. return getattr(self.get_queryset(), 
name)(*args, **kwargs)File 
"/home/den/HTML_Projects/HTML5/test_project/venv/lib/python3.5/site-packages/django/db/models/query.py"
 in create  417. obj.save(force_insert=True, using=self.db)File 
"/home/den/HTML_Projects/HTML5/test_project/orders/models.py" in save  47.  
   super(Order, self).save(*args, **kwargs)File 
"/home/den/HTML_Projects/HTML5/test_project/venv/lib/python3.5/site-packages/django/db/models/base.py"
 in save  729.force_update=force_update, 
update_fields=update_fields)File 
"/home/den/HTML_Projects/HTML5/test_project/venv/lib/python3.5/site-packages/django/db/models/base.py"
 in save_base  759. updated = self._save_table(raw, cls, 
force_insert, force_update, using, update_fields)File 
"/home/den/HTML_Projects/HTML5/test_project/venv/lib/python3.5/site-packages/django/db/transaction.py"
 in __exit__  212. connection.commit()File 
"/home/den/HTML_Projects/HTML5/test_project/venv/lib/python3.5/site-packages/django/db/backends/base/base.py"
 in commit  261. self._commit()File 
"/home/den/HTML_Projects/HTML5/test_project/venv/lib/python3.5/site-packages/django/db/backends/base/base.py"
 in _commit  239. return self.connection.commit()File 
"/home/den/HTML_Projects/HTML5/test_project/venv/lib/python3.5/site-packages/django/db/utils.py"
 in __exit__  89. raise dj_exc_value.with_traceback(traceback) 
from exc_valueFile 
"/home/den/HTML_Projects/HTML5/test_project/venv/lib/python3.5/site-packages/django/db/backends/base/base.py"
 in _commit  239. return self.connection.commit()Exception 
Type: IntegrityError at /checkout/Exception Value: FOREIGN KEY constraint failed

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/3823dd18-7da4-46e5-be63-bfee431b2943%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: IntegrityError: FOREIGN KEY constraint failed: Django 2.0.3

2018-03-22 Thread Seven Reeds
Uh, am I not using the ORM?  If I am not then what must I do to get back on 
track?

I just realized that i didn't include the "Delete" CBV in my original 
post.  I do not have easy access to the code at the moment so what follows 
is a rough approximation of what that code is about and is prolly full of 
errors:

class Delete(DeleteView):
def get:
profile = Profile.objects.get(account_number=self.request.account_number)
user = user.objects.get(pk=profile.user.pk)
user.delete()

In a debugger I can stop on the "user.delete()" line and see that the 
profile and user objects are what I expect them to be.



On Wednesday, March 21, 2018 at 10:34:47 AM UTC-5, Simon Charette wrote:
>
> Hello Anon,
>
> Django doesn't use database level ON DELETE constraints and emulates them 
> in Python
> to dispatch pre_delete/post_delete signals. As long as you're using the 
> ORM to perform
> the deletion you shouldn't encounter constraint violations.
>
> There's a ticket to add support for database level on_delete 
> constraints[0].
>
> Cheers,
> Simon
>
> [0] https://code.djangoproject.com/ticket/21961
>
> Le mercredi 21 mars 2018 10:50:25 UTC-4, Anon Ymous a écrit :
>>
>> Hi,
>>
>> I am learning python by trying to create a small Django app.  I may have 
>> found a bug related to Django and/or sqlite3.  If you are stackoverflow 
>> folks then my initial post is there:  
>> https://stackoverflow.com/questions/49361834/integrityerror-exception-in-deleteview-cbv
>>
>> My app really is just creating a small web site to add users and figure 
>> out how to do CRUD operations in building user profiles.  I am using the 
>> following setup:
>>
>> Django Version: 2.0.3
>> Python Version: 3.6.3
>> Installed Applications:
>> ['django.contrib.admin',
>> 'django.contrib.auth',
>> 'django.contrib.contenttypes',
>> 'django.contrib.sessions',
>> 'django.contrib.messages',
>> 'django.contrib.staticfiles',
>> 'django_extensions',
>> 'django.contrib.sites',
>> 'allauth',
>> 'allauth.account',
>> 'allauth.socialaccount',
>> 'auditlog',
>> 'widget_tweaks',
>> 'Members.apps.MembersConfig']
>> Installed Middleware:
>> ['django.middleware.security.SecurityMiddleware',
>> 'django.contrib.sessions.middleware.SessionMiddleware',
>> 'django.middleware.common.CommonMiddleware',
>> 'django.middleware.csrf.CsrfViewMiddleware',
>> 'django.contrib.auth.middleware.AuthenticationMiddleware',
>> 'django.contrib.messages.middleware.MessageMiddleware',
>> 'django.middleware.clickjacking.XFrameOptionsMiddleware',
>> 'auditlog.middleware.AuditlogMiddleware']
>>
>> I am using sqlite3 as the DB backend..
>>
>> I can create the virgin project, tweak the settings file and do the 
>> initial "makemigrations" and "migrate".  This, of course, creates the 
>> Django "user" table(s).  I went about creating my own "profile" model that 
>> "extends" the User model by creating a "oneToOne" field that points back to 
>> User and specifies an "on_delete=models.CASCADE" clause:
>>
>> class Profile(models.Model):
>> ...
>> user = models.OneToOneField(
>> User,
>> on_delete=models.CASCADE,
>> blank=False,
>> null=False,
>> )
>> ...
>>
>> The thing is the table that is created is given the constraint:
>>
>> FOREIGN KEY(`user_id`) REFERENCES `auth_user`(`id`) DEFERRABLE INITIALLY 
>> DEFERRED,
>>
>> but the "on delete cascade" clause is missing.  
>>
>> I first noticed this when testing a profile delete operation.  I get a 
>> foreign key constraint violation.  Looking into that led me here to you 
>> guys.
>>
>> I have added that clause to the table:
>>
>> FOREIGN KEY(`user_id`) REFERENCES `auth_user`(`id`) on delete cascade 
>> DEFERRABLE INITIALLY DEFERRED,
>>
>> but I still get the constraint violation.  I did more digging last night 
>> and see that at least one of the Django generated "user_*" tables also has 
>> a foreign key relationship back to the "user" table and that is also 
>> missing the cascade clause.
>>
>> My guesses at this instant include:
>>
>>- I have no idea what I am doing
>>- Django or the sqlite3 backend *should* be handling the cascade ops 
>>internally -- but isn't
>>
>> What am I missing?
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/47509400-f086-4f25-b749-c5bf0853f2d4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: IntegrityError: FOREIGN KEY constraint failed: Django 2.0.3

2018-03-21 Thread Simon Charette
Hello Anon,

Django doesn't use database level ON DELETE constraints and emulates them 
in Python
to dispatch pre_delete/post_delete signals. As long as you're using the ORM 
to perform
the deletion you shouldn't encounter constraint violations.

There's a ticket to add support for database level on_delete constraints[0].

Cheers,
Simon

[0] https://code.djangoproject.com/ticket/21961

Le mercredi 21 mars 2018 10:50:25 UTC-4, Anon Ymous a écrit :
>
> Hi,
>
> I am learning python by trying to create a small Django app.  I may have 
> found a bug related to Django and/or sqlite3.  If you are stackoverflow 
> folks then my initial post is there:  
> https://stackoverflow.com/questions/49361834/integrityerror-exception-in-deleteview-cbv
>
> My app really is just creating a small web site to add users and figure 
> out how to do CRUD operations in building user profiles.  I am using the 
> following setup:
>
> Django Version: 2.0.3
> Python Version: 3.6.3
> Installed Applications:
> ['django.contrib.admin',
> 'django.contrib.auth',
> 'django.contrib.contenttypes',
> 'django.contrib.sessions',
> 'django.contrib.messages',
> 'django.contrib.staticfiles',
> 'django_extensions',
> 'django.contrib.sites',
> 'allauth',
> 'allauth.account',
> 'allauth.socialaccount',
> 'auditlog',
> 'widget_tweaks',
> 'Members.apps.MembersConfig']
> Installed Middleware:
> ['django.middleware.security.SecurityMiddleware',
> 'django.contrib.sessions.middleware.SessionMiddleware',
> 'django.middleware.common.CommonMiddleware',
> 'django.middleware.csrf.CsrfViewMiddleware',
> 'django.contrib.auth.middleware.AuthenticationMiddleware',
> 'django.contrib.messages.middleware.MessageMiddleware',
> 'django.middleware.clickjacking.XFrameOptionsMiddleware',
> 'auditlog.middleware.AuditlogMiddleware']
>
> I am using sqlite3 as the DB backend..
>
> I can create the virgin project, tweak the settings file and do the 
> initial "makemigrations" and "migrate".  This, of course, creates the 
> Django "user" table(s).  I went about creating my own "profile" model that 
> "extends" the User model by creating a "oneToOne" field that points back to 
> User and specifies an "on_delete=models.CASCADE" clause:
>
> class Profile(models.Model):
> ...
> user = models.OneToOneField(
> User,
> on_delete=models.CASCADE,
> blank=False,
> null=False,
> )
> ...
>
> The thing is the table that is created is given the constraint:
>
> FOREIGN KEY(`user_id`) REFERENCES `auth_user`(`id`) DEFERRABLE INITIALLY 
> DEFERRED,
>
> but the "on delete cascade" clause is missing.  
>
> I first noticed this when testing a profile delete operation.  I get a 
> foreign key constraint violation.  Looking into that led me here to you 
> guys.
>
> I have added that clause to the table:
>
> FOREIGN KEY(`user_id`) REFERENCES `auth_user`(`id`) on delete cascade 
> DEFERRABLE INITIALLY DEFERRED,
>
> but I still get the constraint violation.  I did more digging last night 
> and see that at least one of the Django generated "user_*" tables also has 
> a foreign key relationship back to the "user" table and that is also 
> missing the cascade clause.
>
> My guesses at this instant include:
>
>- I have no idea what I am doing
>- Django or the sqlite3 backend *should* be handling the cascade ops 
>internally -- but isn't
>
> What am I missing?
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/0a368a90-da44-4af1-a5a1-d680e35d0b5d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


IntegrityError: FOREIGN KEY constraint failed: Django 2.0.3

2018-03-21 Thread Anon Ymous
Hi,

I am learning python by trying to create a small Django app.  I may have 
found a bug related to Django and/or sqlite3.  If you are stackoverflow 
folks then my initial post is there:  
https://stackoverflow.com/questions/49361834/integrityerror-exception-in-deleteview-cbv

My app really is just creating a small web site to add users and figure out 
how to do CRUD operations in building user profiles.  I am using the 
following setup:

Django Version: 2.0.3
Python Version: 3.6.3
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'auditlog',
'widget_tweaks',
'Members.apps.MembersConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'auditlog.middleware.AuditlogMiddleware']

I am using sqlite3 as the DB backend..

I can create the virgin project, tweak the settings file and do the initial 
"makemigrations" and "migrate".  This, of course, creates the Django "user" 
table(s).  I went about creating my own "profile" model that "extends" the 
User model by creating a "oneToOne" field that points back to User and 
specifies an "on_delete=models.CASCADE" clause:

class Profile(models.Model):
...
user = models.OneToOneField(
User,
on_delete=models.CASCADE,
blank=False,
null=False,
)
...

The thing is the table that is created is given the constraint:

FOREIGN KEY(`user_id`) REFERENCES `auth_user`(`id`) DEFERRABLE INITIALLY 
DEFERRED,

but the "on delete cascade" clause is missing.  

I first noticed this when testing a profile delete operation.  I get a 
foreign key constraint violation.  Looking into that led me here to you 
guys.

I have added that clause to the table:

FOREIGN KEY(`user_id`) REFERENCES `auth_user`(`id`) on delete cascade 
DEFERRABLE INITIALLY DEFERRED,

but I still get the constraint violation.  I did more digging last night 
and see that at least one of the Django generated "user_*" tables also has 
a foreign key relationship back to the "user" table and that is also 
missing the cascade clause.

My guesses at this instant include:

   - I have no idea what I am doing
   - Django or the sqlite3 backend *should* be handling the cascade ops 
   internally -- but isn't
   
What am I missing?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/29eb69c4-8f01-4695-b72e-0914ff46422e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.