Opened 5 months ago

Closed 5 months ago

Last modified 5 months ago

#35549 closed Bug (invalid)

The Model's Meta option managed doesn't do what the documentation says it should.

Reported by: Tobi DEGNON Owned by: nobody
Component: Database layer (models, ORM) Version: 5.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I'm not sure what I'm doing wrong here. It's something I've noticed for a while (since Django 4.0), but I didn't think much of it. I thought I was doing something wrong, but I just created a blank new Django 5 project (https://github.com/Tobi-De/django_managed_bug), added a new model with managed = False, and then ran the makemigrations command. It created a migration for a non-managed model, and running migrate created a table for that model.

This is what the documentation says about non-managed models:

If False, no database table creation, modification, or deletion operations will be performed for this model. This is useful if the model represents an existing table or a database view that has been created by some other means. This is the only difference when managed=False. All other aspects of model handling are exactly the same as normal. This includes

Change History (3)

comment:1 by Tobi DEGNON, 5 months ago

Component: UncategorizedDatabase layer (models, ORM)

comment:2 by Simon Charette, 5 months ago

Resolution: invalid
Status: newclosed

I cannot produce with the project you provided. Django creates a migration to track the model but the underlying table is not created as documented.

SQLite version 3.37.0 2021-12-09 01:34:53
Enter ".help" for usage hints.
sqlite> SELECT * FROM django_migrations WHERE app = 'core';
18|core|0001_initial|2024-06-21 16:34:30.110518
sqlite> .tables
auth_group                  auth_user_user_permissions
auth_group_permissions      django_admin_log
auth_permission             django_content_type
auth_user                   django_migrations
auth_user_groups            django_session

sqlmigrate is coherent with that

$ ./manage.py sqlmigrate core 0001
BEGIN;
--
-- Create model Book
--
-- (no-op)
COMMIT;

There is a difference between creating a migration for tracking purposes and actually creating a table on migration.

Please see TicketClosingReasons/UseSupportChannels for further support.

in reply to:  2 comment:3 by Tobi DEGNON, 5 months ago

Replying to Simon Charette:

I cannot produce with the project you provided. Django creates a migration to track the model but the underlying table is not created as documented.

SQLite version 3.37.0 2021-12-09 01:34:53
Enter ".help" for usage hints.
sqlite> SELECT * FROM django_migrations WHERE app = 'core';
18|core|0001_initial|2024-06-21 16:34:30.110518
sqlite> .tables
auth_group                  auth_user_user_permissions
auth_group_permissions      django_admin_log
auth_permission             django_content_type
auth_user                   django_migrations
auth_user_groups            django_session

sqlmigrate is coherent with that

$ ./manage.py sqlmigrate core 0001
BEGIN;
--
-- Create model Book
--
-- (no-op)
COMMIT;

There is a difference between creating a migration for tracking purposes and actually creating a table on migration.

Please see TicketClosingReasons/UseSupportChannels for further support.

My bad, thanks for the clarification, you are right, I was expecting no migration file to be created

Note: See TracTickets for help on using tickets.
Back to Top