[Django] #32171: Unable to use ProjectState and ModelState correctly to make changes to MySQL

6 views
Skip to first unread message

Django

unread,
Nov 4, 2020, 9:58:21 AM11/4/20
to django-...@googlegroups.com
#32171: Unable to use ProjectState and ModelState correctly to make changes to
MySQL
-----------------------------------------+------------------------
Reporter: ganeshkbhat | Owner: nobody
Type: Uncategorized | Status: new
Component: Migrations | Version: 3.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
I am using ProjectState to migrate to a new attributes of a table. I am
trying to understand the ModelState and ProjectState using of migrations
API in Django 3.0.3/3.1.x.

I am unable to migrate to the new state which has new fields. Can someone
help me with the ProjectState and ModelState usage of what to apply for
new model_definition migration to work? The following code does not
migrate to DB but doesnt give any error.

I want to migrate from a DB table state to another state and there are
some metadata _meta.

The current DB state model_state.fields is:

[('id', <django.db.models.fields.AutoField>)]

The future DB state model_state.fields after adding fields_attrs
migrations should be this using the models_definition:

[('id', <django.db.models.fields.AutoField>), ('name',
<django.db.models.fields.CharField>)]

This is the code I am using:

**

from django.db.migrations.state import ProjectState
from django.db.migrations.migration import Migration
from django.db.migrations.state import ModelState
from django.db.migrations import operations

# model_definition is coming from a function as the following object
model_definition = {'__module__': 'testmodule', 'app_label': 'testmodule',
'__unicode__': <function ModelScript.model_create_config.<locals>.<lambda>
at 0x000002047275FF70>, 'attrs': {'name':
<django.db.models.fields.CharField>}, '__doc__': 'SampleModel(id)',
'_meta': <Options for SampleModel>, 'DoesNotExist': <class
'testmodule.SampleModel.DoesNotExist'>, 'MultipleObjectsReturned': <class
'testmodule.SampleModel.MultipleObjectsReturned'>, 'id':
<django.db.models.query_utils.DeferredAttribute object at
0x00000204727F9430>, 'objects':
<django.db.models.manager.ManagerDescriptor object at 0x00000204727F9490>}

model_state = ModelState.from_model(model_definition)

# field_attrs are all the new fields to be migrated

for k,v in field_attrs.items():
model_state.fields.append((k, v))

# Create a fake migration with the CreateModel operation
cm = operations.CreateModel(name=model_state.name,
fields=model_state.fields)

migration = Migration("fake_migration", model_state.app_label)
migration.operations.append(cm)

# SHOULD ProjectState be used for the new definition to be APPLIED to DB
and HOW?
state = ProjectState()
with db_conn.schema_editor(collect_sql=True, atomic=migration.atomic) as
schema_editor:
# Following create_model also doesnot migrate to Mysql DB
# Gives a Table exists Error even with root user of mysql
# schema_editor.create_model(model_definition)

# Following doesnot migrate to the new required state
state = migration.apply(state, schema_editor, collect_sql=True)
# Following gives atomic transaction error if used along with atomic
# following commit commented gives no error but doesnt migrate
# db_conn.commit()**

I have read this and using How to programmatically generate the CREATE
TABLE SQL statement for a given model in Django?

I did try the test cases of Django and it didn't work programmatically. Do
I have to use addfield categorically? Unsure of how to get this working.
Both projectstate and model_create way is not working. I am probably using
this wrong or I am missed something. Any help or resource is welcome on
this.

https://stackoverflow.com/questions/64639768/unable-to-migrate-using-
modelstate-and-projectstate-using-of-migrations-api-in-d

--
Ticket URL: <https://code.djangoproject.com/ticket/32171>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 4, 2020, 2:22:47 PM11/4/20
to django-...@googlegroups.com
#32171: Unable to use ProjectState and ModelState correctly to make changes to
MySQL
-------------------------------+--------------------------------------
Reporter: Ganesh Bhat | Owner: nobody
Type: Uncategorized | Status: closed
Component: Migrations | Version: 3.1
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => invalid


Comment:

Please don't use Trac as a support channel. Closing per
TicketClosingReasons/UseSupportChannels.

--
Ticket URL: <https://code.djangoproject.com/ticket/32171#comment:1>

Reply all
Reply to author
Forward
0 new messages