MySQL table is missing ON DELETE CASCADE constraint

2023-01-20 Thread Dennis Tants

Hey folks,

I recently came across what I think is a bug. Let me try to explain it 
as detailed as possible.
Imagine I have two models, one is called Events, the other is called 
EventAccounts. EventAccounts can't exist without an Event, so I added a 
ForeignKey with the on_delete=models.CASCADE constraint to one field of 
EventAccounts. Should I delete an event, the corresponding event 
accounts will also be deleted. This works just fine when using Django 
itself (or the admin pages). See my models.py.

models.py:

class Events(models.Model):
   ...

class EventAccounts(models.Model):
   ...
  account_event = models.ForeignKey(Events, on_delete=models.CASCADE)
   ...


Now one of my tasks is to cleanup the database every day to delete 
events, which ended more than two weeks ago. I was thinking of creating 
a cronjob to run raw SQL every day. When running this command:
DELETE FROM app_eventtool_events WHERE event_end_date < (NOW() - 
INTERVAL 14 DAY);
I always get informed that there is an existing child and the events do 
not get deleted. Which is why I had a look at the table constraints with:

SHOW CREATE TABLE app_eventtool_eventaccounts;

It seems that the ON DELETE CASCADE statement at the end is missing:
CONSTRAINT 
`app_eventtool_eventa_account_event_id_0ff3718a_fk_app_event` FOREIGN 
KEY (`account_event_id`) REFERENCES `app_eventtool_events` (`event_id`)
If I now delete the above constraint and add a selfmade one, it looks 
like this:
ALTER TABLE app_eventtool_eventaccounts ADD CONSTRAINT 
app_eventtool_events_account_event_id FOREIGN KEY (account_event_id) 
REFERENCES app_eventtool_events (event_id) ON DELETE CASCADE ON UPDATE 
NO ACTION;
CONSTRAINT `app_eventtool_events_account_event_id` FOREIGN KEY 
(`account_event_id`) REFERENCES `app_eventtool_events` (`event_id`) ON 
DELETE CASCADE ON UPDATE NO ACTION
As you can see, ON DELETE CASCADE is now present. Now I am also able to 
delete the events with raw SQL as mentioned above. This is reproducible 
when dropping and creating the database fully. Updating from version 
4.0.6 to version 4.1.5 did not solve the problem.


Furthermore I started testing a bit with the on_delete statement in 
models.py. I changed models.CASCADE to models.RESTRICT and created a new 
migration. The migration file itself looks fine. But when trying to get 
the raw SQL of the migration via:

python3 manage.py sqlmigrate app_eventtool 0002

it basically results in an empty migration:

--
-- Alter field account_event on eventaccounts
--
-- (no-op)


Maybe this is expected behaviour, but for me it seems like a bug. Can 
anyone confirm/deny this?


Thanks in advance,
Dennis

--
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/cb989abd-cb08-8093-668a-03b756149be1%40uni-bremen.de.


Re: MySQL table is missing ON DELETE CASCADE constraint

2023-01-20 Thread Bhuvnesh Sharma
Hi dennis,
You can open a ticket on the trac (https://code.djangoproject.com/query)
for whatever issue you are facing.It will be easier to understand there.

Regards
Bhuvnesh


On Fri, Jan 20, 2023, 8:36 PM Dennis Tants  wrote:

> Hey folks,
>
> I recently came across what I think is a bug. Let me try to explain it as
> detailed as possible.
> Imagine I have two models, one is called Events, the other is called
> EventAccounts. EventAccounts can't exist without an Event, so I added a
> ForeignKey with the on_delete=models.CASCADE constraint to one field of
> EventAccounts. Should I delete an event, the corresponding event accounts
> will also be deleted. This works just fine when using Django itself (or the
> admin pages). See my models.py.
> models.py:
>
> class Events(models.Model):
>...
>
> class EventAccounts(models.Model):
>...
>   account_event = models.ForeignKey(Events, on_delete=models.CASCADE)
>...
>
>
> Now one of my tasks is to cleanup the database every day to delete events,
> which ended more than two weeks ago. I was thinking of creating a cronjob
> to run raw SQL every day. When running this command:
>
> DELETE FROM app_eventtool_events WHERE event_end_date < (NOW() - INTERVAL
> 14 DAY);
>
> I always get informed that there is an existing child and the events do
> not get deleted. Which is why I had a look at the table constraints with:
>
> SHOW CREATE TABLE app_eventtool_eventaccounts;
>
> It seems that the ON DELETE CASCADE statement at the end is missing:
>
> CONSTRAINT `app_eventtool_eventa_account_event_id_0ff3718a_fk_app_event`
> FOREIGN KEY (`account_event_id`) REFERENCES `app_eventtool_events`
> (`event_id`)
>
> If I now delete the above constraint and add a selfmade one, it looks like
> this:
>
> ALTER TABLE app_eventtool_eventaccounts ADD CONSTRAINT
> app_eventtool_events_account_event_id FOREIGN KEY (account_event_id)
> REFERENCES app_eventtool_events (event_id) ON DELETE CASCADE ON UPDATE NO
> ACTION;
> CONSTRAINT `app_eventtool_events_account_event_id` FOREIGN KEY
> (`account_event_id`) REFERENCES `app_eventtool_events` (`event_id`) ON
> DELETE CASCADE ON UPDATE NO ACTION
>
> As you can see, ON DELETE CASCADE is now present. Now I am also able to
> delete the events with raw SQL as mentioned above. This is reproducible
> when dropping and creating the database fully. Updating from version 4.0.6
> to version 4.1.5 did not solve the problem.
>
> Furthermore I started testing a bit with the on_delete statement in
> models.py. I changed models.CASCADE to models.RESTRICT and created a new
> migration. The migration file itself looks fine. But when trying to get the
> raw SQL of the migration via:
>
> python3 manage.py sqlmigrate app_eventtool 0002
>
> it basically results in an empty migration:
>
> --
> -- Alter field account_event on eventaccounts
> --
> -- (no-op)
>
>
> Maybe this is expected behaviour, but for me it seems like a bug. Can
> anyone confirm/deny this?
>
> Thanks in advance,
> Dennis
>
> --
> 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 view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/cb989abd-cb08-8093-668a-03b756149be1%40uni-bremen.de
> 
> .
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2BZJHEpAEUnrGz31Ji%3DGWdJ7b-e49%2BPxpjLCfvq5Zq8-qb0bjg%40mail.gmail.com.


Re: MySQL table is missing ON DELETE CASCADE constraint

2023-01-20 Thread Jason
https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.CASCADE

> Django emulates the behavior of the SQL constraint ON DELETE CASCADE and 
also deletes the object containing the ForeignKey.

this is specified in the docs.  

https://code.djangoproject.com/ticket/21961
On Friday, January 20, 2023 at 10:44:48 AM UTC-5 bhuvn...@gmail.com wrote:

> Hi dennis,
> You can open a ticket on the trac (https://code.djangoproject.com/query) 
> for whatever issue you are facing.It will be easier to understand there.
>
> Regards
> Bhuvnesh 
>
>
> On Fri, Jan 20, 2023, 8:36 PM Dennis Tants  wrote:
>
>> Hey folks, 
>>
>> I recently came across what I think is a bug. Let me try to explain it as 
>> detailed as possible. 
>> Imagine I have two models, one is called Events, the other is called 
>> EventAccounts. EventAccounts can't exist without an Event, so I added a 
>> ForeignKey with the on_delete=models.CASCADE constraint to one field of 
>> EventAccounts. Should I delete an event, the corresponding event accounts 
>> will also be deleted. This works just fine when using Django itself (or the 
>> admin pages). See my models.py. 
>> models.py: 
>>
>> class Events(models.Model): 
>>... 
>>
>> class EventAccounts(models.Model): 
>>... 
>>   account_event = models.ForeignKey(Events, on_delete=models.CASCADE) 
>>... 
>>
>>
>> Now one of my tasks is to cleanup the database every day to delete 
>> events, which ended more than two weeks ago. I was thinking of creating a 
>> cronjob to run raw SQL every day. When running this command: 
>>
>> DELETE FROM app_eventtool_events WHERE event_end_date < (NOW() - INTERVAL 
>> 14 DAY); 
>>
>> I always get informed that there is an existing child and the events do 
>> not get deleted. Which is why I had a look at the table constraints with: 
>>
>> SHOW CREATE TABLE app_eventtool_eventaccounts; 
>>
>> It seems that the ON DELETE CASCADE statement at the end is missing: 
>>
>> CONSTRAINT `app_eventtool_eventa_account_event_id_0ff3718a_fk_app_event` 
>> FOREIGN KEY (`account_event_id`) REFERENCES `app_eventtool_events` 
>> (`event_id`) 
>>
>> If I now delete the above constraint and add a selfmade one, it looks 
>> like this: 
>>
>> ALTER TABLE app_eventtool_eventaccounts ADD CONSTRAINT 
>> app_eventtool_events_account_event_id FOREIGN KEY (account_event_id) 
>> REFERENCES app_eventtool_events (event_id) ON DELETE CASCADE ON UPDATE NO 
>> ACTION; 
>> CONSTRAINT `app_eventtool_events_account_event_id` FOREIGN KEY 
>> (`account_event_id`) REFERENCES `app_eventtool_events` (`event_id`) ON 
>> DELETE CASCADE ON UPDATE NO ACTION 
>>
>> As you can see, ON DELETE CASCADE is now present. Now I am also able to 
>> delete the events with raw SQL as mentioned above. This is reproducible 
>> when dropping and creating the database fully. Updating from version 4.0.6 
>> to version 4.1.5 did not solve the problem. 
>>
>> Furthermore I started testing a bit with the on_delete statement in 
>> models.py. I changed models.CASCADE to models.RESTRICT and created a new 
>> migration. The migration file itself looks fine. But when trying to get the 
>> raw SQL of the migration via: 
>>
>> python3 manage.py sqlmigrate app_eventtool 0002 
>>
>> it basically results in an empty migration: 
>>
>> -- 
>> -- Alter field account_event on eventaccounts 
>> -- 
>> -- (no-op) 
>>
>>
>> Maybe this is expected behaviour, but for me it seems like a bug. Can 
>> anyone confirm/deny this? 
>>
>> Thanks in advance, 
>> Dennis 
>>
>> -- 
>> 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...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/cb989abd-cb08-8093-668a-03b756149be1%40uni-bremen.de
>>  
>> 
>> .
>>
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/67684c77-0d05-4514-b675-abe6e0eb2b20n%40googlegroups.com.


Re: Please i need help in creating a Login page with Django

2023-01-20 Thread Aditi Dwivedi
You have to first create a superuser in your django project.
It is same as creating a root user in Linux or admin user in windows.
Super user can be created as..
1. Run command in django terminal
 Python manage.py createsuperuser


2. It will ask for user name, emai and password.

3. Run command
Python manage.py runserver.


Hope this helps.

On Thu, 19 Jan 2023, 6:51 pm Radhika Soni,  wrote:

> Hello,
> You can take help from udemy or coursera courses.
>
> On Thu, Jan 19, 2023, 4:36 PM Hubert Kanyamahanga 
> wrote:
>
>> Please share what you have been able to do, where you are blocked and
>> from there we can guide you, because, we don't even know which videos you
>> have been viewing!
>>
>> On Wednesday, January 18, 2023 at 11:56:45 PM UTC+1 macca...@gmail.com
>> wrote:
>>
>>> I am trying everything possible to create a basic login page with
>>> Django., i have tried so many videos and online tutorial, but still can't
>>> make anything meaningful out of it. Can anyone please take me through a
>>> systematic process please, since i am new here and want to take Django to
>>> be my friend
>>
>> --
>> 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 view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/61f6c9f9-2973-4572-ae51-8689d6521292n%40googlegroups.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 view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAGhU6zNsAmutLvJRFdHMPOU8ZKS4Oywha%2B%3DoOWkKAUw4tsh61A%40mail.gmail.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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHBdd%2BAnpzf1N7VxS7Ue4OdEtzBzUTDf%3DizDmRkwA7xfvpdcZA%40mail.gmail.com.