Django migrate throws AttributeError: 'str' object has no attribute '_meta' - django

After doing some extensive changes in the DB schema, I ran makemigrations. It created migrations successfully. But then migrate failed with:
AttributeError: 'str' object has no attribute '_meta'
What went wrong?
Here is the full traceback:
$ ./manage.py migrate
Operations to perform:
Apply all migrations: account, admin, auth, clients, contenttypes, ...<snip>
Running migrations:
Applying clients.0004_auto_20200910_1241...Traceback (most recent call last):
File "./manage.py", line 21, in <module>
main()
File "./manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "...<snip>.../lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "...<snip>.../lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "...<snip>.../lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "...<snip>.../lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "...<snip>.../lib/python3.8/site-packages/django/core/management/base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "...<snip>.../lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 243, in handle
post_migrate_state = executor.migrate(
File "...<snip>.../lib/python3.8/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "...<snip>.../lib/python3.8/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "...<snip>.../lib/python3.8/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "...<snip>.../lib/python3.8/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "...<snip>.../lib/python3.8/site-packages/django/db/migrations/operations/fields.py", line 236, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "...<snip>.../lib/python3.8/site-packages/django/db/backends/sqlite3/schema.py", line 138, in alter_field
super().alter_field(model, old_field, new_field, strict=strict)
File "...<snip>.../lib/python3.8/site-packages/django/db/backends/base/schema.py", line 553, in alter_field
old_field.remote_field.through._meta.auto_created and
AttributeError: 'str' object has no attribute '_meta'
Somewhat similar to this question: Django makemigrations AttributeError: 'str' object has no attribute '_meta' but my makemigrations went ok and only failed at migrate command.

Turns out that in one of the migrations there were 2 changes created in the wrong order:
a model being deleted
an M2M field which used the deleted model for the through param was altered (to use a different "through model").
Deleting the migrations.DeleteModel step from the migration file solved the issue.
Of course, I had to create a new migration that would actually delete the stale model, but that went perfectly.
Not sure if simply moving the order of the migration actions in the migration file would resolve the issue, probably.
Could be a bug in Django, needs some more research.

Related

Not able to do the migrate in Django, facing Attribute Error: 'str' object has no attribute '_meta'

I made a completely new project using the django-admin startproject and I also made an app using django-admin startapp and I am able to apply the initial migrations but not able to do the migrate. and I didn't add any code into the project. It's completely a new project.
python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying auth.0013_user_following...Traceback (most recent call last):
File "G:\My Drive\Django 4 by example\myshop\manage.py", line 22, in <module>
main()
File "G:\My Drive\Django 4 by example\myshop\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\admin\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
utility.execute()
File "C:\Users\admin\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 440, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\admin\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 402, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\admin\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 448, in execute
output = self.handle(*args, **options)
File "C:\Users\admin\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 96, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\admin\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\commands\migrate.py", line 349, in handle
post_migrate_state = executor.migrate(
File "C:\Users\admin\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\executor.py", line 135, in migrate
state = self._migrate_all_forwards(
File "C:\Users\admin\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\executor.py", line 167, in _migrate_all_forwards
state = self.apply_migration(
File "C:\Users\admin\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\executor.py", line 252, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\admin\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\migration.py", line 130, in apply
operation.database_forwards(
File "C:\Users\admin\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\operations\fields.py", line 108, in database_forwards
schema_editor.add_field(
File "C:\Users\admin\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\sqlite3\schema.py", line 383, in add_field
if field.many_to_many and field.remote_field.through._meta.auto_created:
AttributeError: 'str' object has no attribute '_meta'
I uninstalled all the packages which I installed for the project, uninstalled Django as well, and deleted all the migrations of all the projects but still it's not working.

Django renaming class casued AttributeError: 'str' object has no attribute '_meta'

How can I solve this issue, I renamed a through Model class and now getting the following error when trying to migrate:
(django_env) UKC02TQH6UHV22:pcc_django rki23$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, microblog, project_portal, sessions, url_tree
Running migrations:
Applying project_portal.0012_auto_20200205_1241...Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 234, in handle
fake_initial=fake_initial,
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/db/migrations/operations/fields.py", line 249, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 524, in alter_field
not new_field.remote_field.through._meta.auto_created):
AttributeError: 'str' object has no attribute '_meta'
(django_env) UKC02TQH6UHV22:pcc_django rki23$
What do I need to do to fix this please?
THis is the migration file that is causing the issue. The name change was for a many to many field.
# Generated by Django 2.2.5 on 2020-02-05 12:41
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('project_portal', '0011_auto_20200127_0953'),
]
operations = [
migrations.RenameModel(
old_name='CategorySatisfied',
new_name='TaskComplete',
),
migrations.AlterField(
model_name='updatecategory',
name='projects',
field=models.ManyToManyField(blank=True, related_name='task_complete', through='project_portal.CategorySatisfied', to='project_portal.Project'),
),
]

Django database and migration error, ValueError: Related model 'Users.user' cannot be resolved

I am facing a very very strange situation with my Django (in general). I'm using Django 2.2 and Python 3.6.7.
Every single time I create a new project, using:
django-admin startproject myproject
and trying to migrate it, I got this error :
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying admin.0004_auto_20190630_1438... OK
Applying admin.0005_auto_20190630_1441...Traceback (most recent call last):
File "C:\Dev\Django\myproject\manage.py", line 21, in <module>
main()
File "C:\Dev\Django\myproject\manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Dev\Django\Env\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Dev\Django\Env\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Dev\Django\Env\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Dev\Django\Env\lib\site-packages\django\core\management\base.py", line 364, in execute
output = self.handle(*args, **options)
File "C:\Dev\Django\Env\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Dev\Django\Env\lib\site-packages\django\core\management\commands\migrate.py", line 234,
in handle
fake_initial=fake_initial,
File "C:\Dev\Django\Env\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake,
fake_initial=fake_initial)
File "C:\Dev\Django\Env\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:\Dev\Django\Env\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Dev\Django\Env\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Dev\Django\Env\lib\site-packages\django\db\migrations\operations\fields.py", line 249, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "C:\Dev\Django\Env\lib\site-packages\django\db\backends\sqlite3\schema.py", line 137, in alter_field
super().alter_field(model, old_field, new_field, strict=strict)
File "C:\Dev\Django\Env\lib\site-packages\django\db\backends\base\schema.py", line 507, in alter_field
new_db_params = new_field.db_parameters(connection=self.connection)
File "C:\Dev\Django\Env\lib\site-packages\django\db\models\fields\related.py", line 966, in db_parameters
return {"type": self.db_type(connection), "check": self.db_check(connection)}
File "C:\Dev\Django\Env\lib\site-packages\django\db\models\fields\related.py", line 963, in db_type
return self.target_field.rel_db_type(connection=connection)
File "C:\Dev\Django\Env\lib\site-packages\django\db\models\fields\related.py", line 878, in target_field
return self.foreign_related_fields[0]
File "C:\Dev\Django\Env\lib\site-packages\django\db\models\fields\related.py", line 632, in foreign_related_fields
return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
File "C:\Dev\Django\Env\lib\site-packages\django\db\models\fields\related.py", line 619, in related_fields
self._related_fields = self.resolve_related_fields()
File "C:\Dev\Django\Env\lib\site-packages\django\db\models\fields\related.py", line 604, in resolve_related_fields
raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
ValueError: Related model 'Users.user' cannot be resolved
this occurs everytime I try to migrate all my projects, even all old projects.
Is this some kind of django bug? or did I configure it improperly? I also use the default django settings (without even touching anything).
I messed up by this error. I cannot do any migrations to all my projects.
I have uninstalled django and reinstalled it back, but nothing changed, I got the same error.
A few moments ago I created a custom user model (AbstractBaseUser) in my project. And it was running perfectly. But I when I try to input data to any model in the admin panel, I got error FOREIGN KEY constraint failed (whereas I had only one model, AbstractBaseUser model). I didn't know what to do with that, And I deleted that project.
Afterwards, I created a new project (for learning purposes), and trying migrate it, this critical serious error appeared.
Do guys have any solution to overcome this? Please help me out of this problem

Django - migrate before setting custom user model, error message

I have forgotten to set my custom user model in the settings file and migrate my project first time. Now i am trying to make migrations again after setting my custom user model with errors -> ... django.db.utils.ProgrammingError: relation "custom_auth_customuser" does not exist ...
I've learned that this is a common error message but i could not understand how to solve it. I have no 'migrations' folder anywhere... Where is the schema that i will change manually?
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, taggit
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying admin.0001_initial...Traceback (most recent call last):
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "custom_auth_customuser" does not exist
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/core/management/base.py", line 305, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/core/management/base.py", line 356, in execute
output = self.handle(*args, **options)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 202, in handle
targets, plan, fake=fake, fake_initial=fake_initial
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/db/migrations/executor.py", line 97, in migrate
state = self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/db/migrations/executor.py", line 132, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/db/migrations/executor.py", line 236, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 92, in __exit__
self.execute(sql)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 112, in execute
cursor.execute(sql, params)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "custom_auth_customuser" does not exist
If I unterstand your problem correctly you should try this steps:
Create a mirgrations folder containing __init__.py in your app
Run makemigrations and migrate

Django AbstractUser migrate cannot be resolved

I want add a field Django User. So I check this document: https://docs.djangoproject.com/en/1.8/topics/auth/customizing/
I decide use AbstractUser to solve my problem.
Add:
class MyUser(AbstractUser):
nickname = models.CharField(max_length=64, blank=False)
to muse.models.
and add AUTH_USER_MODEL = "muse.MyUser" to the settings.py file.
But when I migrate always have errors:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: admin, contenttypes, muse, auth, sessions
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying admin.0001_initial...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.7/site-packages/django/core/management/commands/migrate.py", line 221, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 147, in apply_migration
state = migration.apply(state, schema_editor)
File "/Library/Python/2.7/site-packages/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Library/Python/2.7/site-packages/django/db/migrations/operations/models.py", line 59, in database_forwards
schema_editor.create_model(model)
File "/Library/Python/2.7/site-packages/django/db/backends/base/schema.py", line 232, in create_model
definition, extra_params = self.column_sql(model, field)
File "/Library/Python/2.7/site-packages/django/db/backends/base/schema.py", line 131, in column_sql
db_params = field.db_parameters(connection=self.connection)
File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py", line 1989, in db_parameters
return {"type": self.db_type(connection), "check": []}
File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py", line 1980, in db_type
rel_field = self.related_field
File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py", line 1886, in related_field
return self.foreign_related_fields[0]
File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py", line 1620, in foreign_related_fields
return tuple(rhs_field for lhs_field, rhs_field in self.related_fields)
File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py", line 1607, in related_fields
self._related_fields = self.resolve_related_fields()
File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py", line 1592, in resolve_related_fields
raise ValueError('Related model %r cannot be resolved' % self.rel.to)
ValueError: Related model 'muse.MyUser' cannot be resolved
What's wrong ?
In my case a tricky workaround was available and I don't suggest it because my case was so special. I had my own database management and was putting Django query builder aside for reasons (It was a university project and it was not allowed). I manipulated my database structure manually and had my own user model and authentication back-end.
The workaround was to create migrations and then to remove their operations manually. After creating migrations I had to disable AUTH_USER_MODEL, migrate and then enable the setting to make the authentication system work.
Hope it was helpful.