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'),
),
]
Related
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.
hey I have working on some project now I want to add IP address fields in every model
ip_address = models.GenericIPAddressField()
adding like this
now when I migrate it I got an error
like this
if value and ':' in value:
TypeError: argument of type 'int' is not iterable
0005_remove_documentvault_ip_address
class Migration(migrations.Migration):
dependencies = [
('master', '0005_remove_documentvault_ip_address'),
]
operations = [
migrations.AddField(
model_name='documentvault',
name='ip_address',
field=models.GenericIPAddressField(default=1),
preserve_default=False,
),
]
Erorr Message:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, master, sessions, users
Running migrations:
Applying master.0006_documentvault_ip_address...Traceback (most recent call last):
File "./manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 203, in handle
fake_initial=fake_initial,
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/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 "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/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 "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 84, in database_forwards
field,
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/backends/mysql/schema.py", line 42, in add_field
super().add_field(model, field)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 421, in add_field
definition, params = self.column_sql(model, field, include_default=True)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 168, in column_sql
default_value = self.effective_default(field)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 239, in effective_default
return field.get_db_prep_save(default, self.connection)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 790, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1936, in get_db_prep_value
value = self.get_prep_value(value)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1943, in get_prep_value
if value and ':' in value:
TypeError: argument of type 'int' is not iterable
The migration (and perhaps your model too) has default=1. That's not a valid IP address.
You can either replace the default in your model with a valid IP address, or remove the default and set null=True.
Then delete the 0006 migration (back it up first to be safe, or make sure it's in version control). Finally, rerun makemigrations and migrate.
After importing Psycopg2==2.7.4 into my server, I can no longer run my server properly. After adding new fields, this is what happens now in my terminal whenever I try to make migrate after makemigrations(python manage.py migrate):
Apply all migrations: admin, auth, buddysapp, contenttypes, oauth2_provider, sessions, social_django
Running migrations:
Applying buddysapp.0038_auto_20180321_0025...Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/hsi/Desktop/myvirtualenv/buddys/lib/python3.6/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/Users/hsi/Desktop/myvirtualenv/buddys/lib/python3.6/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/hsi/Desktop/myvirtualenv/buddys/lib/python3.6/site-packages/django/core/management/base.py", line 305, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/hsi/Desktop/myvirtualenv/buddys/lib/python3.6/site-packages/django/core/management/base.py", line 356, in execute
output = self.handle(*args, **options)
File "/Users/hsi/Desktop/myvirtualenv/buddys/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 202, in handle
targets, plan, fake=fake, fake_initial=fake_initial
File "/Users/hsi/Desktop/myvirtualenv/buddys/lib/python3.6/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/hsi/Desktop/myvirtualenv/buddys/lib/python3.6/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/hsi/Desktop/myvirtualenv/buddys/lib/python3.6/site-packages/django/db/migrations/executor.py", line 237, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/hsi/Desktop/myvirtualenv/buddys/lib/python3.6/site-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Users/hsi/Desktop/myvirtualenv/buddys/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 84, in database_forwards
field,
File "/Users/hsi/Desktop/myvirtualenv/buddys/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 231, in add_field
self._remake_table(model, create_fields=[field])
File "/Users/hsi/Desktop/myvirtualenv/buddys/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 113, in _remake_table
self.effective_default(field)
File "/Users/hsi/Desktop/myvirtualenv/buddys/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 68, in quote_value
raise ValueError("Cannot quote parameter value %r of type %s" % (value, type(value)))
ValueError: Cannot quote parameter value <psycopg2._json.Json object at 0x10ea5e748> of type <class 'psycopg2._json.Json'>
Any ideas, let me know.
Update: Migration file 0038
rom __future__ import unicode_literals
import buddysapp.models
import django.contrib.postgres.fields.jsonb
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('buddysapp', '0037_auto_20180318_1816'),
]
operations = [
migrations.AddField(
model_name='dispensary',
name='businessHours',
field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=buddysapp.models.business_hours_default, null=True),
),
migrations.AlterField(
model_name='order',
name='status',
field=models.IntegerField(choices=[(1, 'Your Order Is Being Picked Right Off The Plant!'), (2, 'Picked, Weighed, And Ready For Delivery.'), (3, 'The Best Bud Is Heading Straight To Your Door.'), (4, 'Delivered'), (0, 'Canceled/Refunded')]),
),
]
You can’t use a JSONField from django.contrib.postgres with an Sqlite3 database.
You need to either set up a PostgreSQL database and update your DATABASES setting, or use a different field in your model.
I have a model with a foreign key field. I want to give the foreign key a related name. However, migration fails because of the following error:
IndexError: list index out of range
I'm using Django==1.10.5
Database backend is PostgreSQL
models.py Before/After migrations
Before:
class Contest_participants(models.Model):
contest = models.ForeignKey(Contest)
After:
class Contest_participants(models.Model):
contest = models.ForeignKey(Contest, related_name='contests')
The makemigrations command works fine, but the migrate command fails with the above noted error.
Complete Traceback Error:
Running migrations:
Applying teams.0013_auto_20171031_1225...Traceback (most recent call last):
File "manage.py", line 19, in <module>
execute_from_command_line(sys.argv)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 204, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 495, in alter_field
old_db_params, new_db_params, strict)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/backends/postgresql/schema.py", line 117, in _alter_field
new_db_params, strict,
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 504, in _alter_field
fk_names = self._constraint_names(model, [old_field.column], foreign_key=True)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 931, in _constraint_names
constraints = self.connection.introspection.get_constraints(cursor, model._meta.db_table)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/backends/postgresql/introspection.py", line 178, in get_constraints
"foreign_key": tuple(used_cols[0].split(".", 1)) if kind.lower() == "foreign key" else None,
IndexError: list index out of range
EDIT: Migrations
class Migration(migrations.Migration):
dependencies = [
('teams', '0012_auto_20170904_1524'),
]
operations = [
migrations.AlterField(
model_name='contest_participants',
name='contest',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='contests', to='contest.Contest'),
),
]
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.