alembic autogenerate after primary key rename failing - python-3.x

Against better judgment, I decided to rename the primary column in my databases from id to key, as I didn't want to clash with Python's id function.
I managed to use alembic to rename the column by using batch migrations, as I'm using an SQLite backend.
Now when adding a new table to my model, alembic manages to detect the new table and then crashes with the following:
sqlalchemy.exc.NoReferencedColumnError: Could not initialize target column for ForeignKey 'tests.id' on table 'runs': table 'tests' has no column named 'id'
I have a table named tests and a table named runs which are linked via a foreign key:
class Base():
key = Column(Integer, primary_key=True)
BASE = declarative_base(cls=Base)
class Test(BASE):
runs = relationship("Run", back_populates="test",
cascade="all, delete, delete-orphan")
class Run(BASE):
test_id = Column(Integer, ForeignKey("tests.key"))
test = relationship("Test", back_populates="runs")
It looks like alembic is trying to recreate all of the migrations when trying to autogenerate. Or at least the table creation.
Am I doing something wrong? Or is this a bug in alembic?
Following is the full command output with my paths redacted.
$ alembic -n testing revision --autogenerate -m "Add sessions table"
INFO [alembic.runtime.migration] Context impl SQLiteImpl.
INFO [alembic.autogenerate.compare] Detected added table 'sessions'
Traceback (most recent call last):
File "bin/alembic", line 10, in <module>
sys.exit(main())
File "lib64/python3.6/site-packages/alembic/config.py", line 540, in main
CommandLine(prog=prog).main(argv=argv)
File "lib64/python3.6/site-packages/alembic/config.py", line 534, in main
self.run_cmd(cfg, options)
File "lib64/python3.6/site-packages/alembic/config.py", line 514, in run_cmd
**dict((k, getattr(options, k, None)) for k in kwarg)
File "lib64/python3.6/site-packages/alembic/command.py", line 197, in revision
script_directory.run_env()
File "lib64/python3.6/site-packages/alembic/script/base.py", line 475, in run_env
util.load_python_file(self.dir, "env.py")
File "lib64/python3.6/site-packages/alembic/util/pyfiles.py", line 90, in load_python_file
module = load_module_py(module_id, path)
File "lib64/python3.6/site-packages/alembic/util/compat.py", line 177, in load_module_py
spec.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "alembic/env.py", line 71, in <module>
run_migrations_online()
File "alembic/env.py", line 65, in run_migrations_online
context.run_migrations()
File "<string>", line 8, in run_migrations
File "lib64/python3.6/site-packages/alembic/runtime/environment.py", line 839, in run_migrations
self.get_context().run_migrations(**kw)
File "lib64/python3.6/site-packages/alembic/runtime/migration.py", line 351, in run_migrations
for step in self._migrations_fn(heads, self):
File "lib64/python3.6/site-packages/alembic/command.py", line 173, in retrieve_migrations
revision_context.run_autogenerate(rev, context)
File "lib64/python3.6/site-packages/alembic/autogenerate/api.py", line 433, in run_autogenerate
self._run_environment(rev, migration_context, True)
File "lib64/python3.6/site-packages/alembic/autogenerate/api.py", line 473, in _run_environment
autogen_context, migration_script
File "lib64/python3.6/site-packages/alembic/autogenerate/compare.py", line 25, in _populate_migration_script
_produce_net_changes(autogen_context, upgrade_ops)
File "lib64/python3.6/site-packages/alembic/autogenerate/compare.py", line 51, in _produce_net_changes
autogen_context, upgrade_ops, schemas
File "lib64/python3.6/site-packages/alembic/util/langhelpers.py", line 303, in go
fn(*arg, **kw)
File "lib64/python3.6/site-packages/alembic/autogenerate/compare.py", line 83, in _autogen_for_tables
autogen_context,
File "lib64/python3.6/site-packages/alembic/autogenerate/compare.py", line 225, in _compare_tables
metadata_table,
File "lib64/python3.6/site-packages/alembic/util/langhelpers.py", line 303, in go
fn(*arg, **kw)
File "lib64/python3.6/site-packages/alembic/autogenerate/compare.py", line 994, in _compare_foreign_keys
for fk in conn_fks
File "lib64/python3.6/site-packages/alembic/autogenerate/compare.py", line 994, in <genexpr>
for fk in conn_fks
File "lib64/python3.6/site-packages/alembic/autogenerate/compare.py", line 400, in __init__
) = _fk_spec(const)
File "lib64/python3.6/site-packages/alembic/util/sqla_compat.py", line 77, in _fk_spec
target_schema = constraint.elements[0].column.table.schema
File "lib64/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 855, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
File "lib64/python3.6/site-packages/sqlalchemy/sql/schema.py", line 2039, in column
colname,
sqlalchemy.exc.NoReferencedColumnError: Could not initialize target column for ForeignKey 'tests.id' on table 'runs': table 'tests' has no column named 'id'

The issue was in the database itself. My previous migration script didn't update the ForeignKey constraint by dropping the constraint and recreating on the new key.
And since SQLite by default doesn't respect foreign keys The upgrade passed, but the database still had the old constraint in it.
By calling the drop_constraint and the create_foreign_key in the upgrade script and using the method described here to specify the unnamed constraint, I managed to run the upgrade script correctly on my test database and run autogenerate correctly.
Subsequentially, I added a check to foreign keys as described in the SQLAlchemy docs, so next time such a change would cause the upgrade to fail if the foreign keys don't match.

Related

DBT workflow on Databricks fails: AttributeError in object SeedNode

Today our DBT workflow in databricks failed. The workflow runs as:
dbt run --target workflow --project-dir dbt/projectdir/ --profiles-dir dbt/
Any suggestions what could be wrong or how to fix it?
Version reported in Databricks logs:
Running with dbt=1.4.1
The error message below:
'SeedNode' object has no attribute 'depends_on'
09:59:17 Traceback (most recent call last):
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/dbt/main.py", line 135, in main
results, succeeded = handle_and_check(args)
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/dbt/main.py", line 198, in handle_and_check
task, res = run_from_args(parsed)
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/dbt/main.py", line 245, in run_from_args
results = task.run()
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/dbt/task/runnable.py", line 454, in run
self._runtime_initialize()
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/dbt/task/runnable.py", line 165, in _runtime_initialize
super()._runtime_initialize()
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/dbt/task/runnable.py", line 94, in _runtime_initialize
self.load_manifest()
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/dbt/task/runnable.py", line 81, in load_manifest
self.manifest = ManifestLoader.get_full_manifest(self.config)
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/dbt/parser/manifest.py", line 203, in get_full_manifest
manifest = loader.load()
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/dbt/parser/manifest.py", line 339, in load
self.parse_project(
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/dbt/parser/manifest.py", line 467, in parse_project
parser.parse_file(block)
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/dbt/parser/base.py", line 425, in parse_file
self.parse_node(file_block)
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/dbt/parser/base.py", line 386, in parse_node
self.render_update(node, config)
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/dbt/parser/base.py", line 363, in render_update
self.update_parsed_node_config(node, config, context=context)
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/dbt/parser/base.py", line 336, in update_parsed_node_config
get_rendered(hook.sql, context, parsed_node, capture_macros=True)
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/dbt/clients/jinja.py", line 590, in get_rendered
return render_template(template, ctx, node)
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/dbt/clients/jinja.py", line 545, in render_template
return template.render(ctx)
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/jinja2/environment.py", line 1301, in render
self.environment.handle_exception()
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/jinja2/environment.py", line 936, in handle_exception
raise rewrite_traceback_stack(source=source)
File "", line 1, in top-level template code
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/jinja2/sandbox.py", line 393, in call
return __context.call(__obj, *args, **kwargs)
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/dbt/clients/jinja.py", line 328, in call
with self.track_call():
File "/usr/lib/python3.9/contextlib.py", line 117, in enter
return next(self.gen)
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/dbt/clients/jinja.py", line 319, in track_call
self.node.depends_on.add_macro(unique_id)
AttributeError: 'SeedNode' object has no attribute 'depends_on'
Got the same issue but I am on snowflake
Seems this was a version issue. Explicitly setting the task to use an older version seems to have solved it:
dbt-core<=1.3.1
dbt-databricks<=1.3.1
This can be set in the Databricks workflow task settings.
I'm not sure which is the last version that would work, but 1.3.1 at least works in our case.

wagtail: Locale_ID cannot be null

I'm trying to deploy a website with wagtail 2.11 on pythonanywhere. However, I'm unable to save any page models in the frontend.
The cause seems to be that in my 'home'-app, migration 0002_create_homepage.py cannot be not applied. Trying so gives me this error (IntegrityError: (1048, "Column 'locale_id' cannot be null")):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/core/management/base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 243, in handle
post_migrate_state = executor.migrate(
File "/home/yogagarten/.virtualenvs/myvirtualenv/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 "/home/yogagarten/.virtualenvs/myvirtualenv/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 "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/db/migrations/migration.py", line 121, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/db/migrations/operations/special.py", line 190, in database_forwards
self.code(from_state.apps, schema_editor)
File "/home/yogagarten/yogagarten.pythonanywhere.com/home/migrations/0002_create_homepage.py", line 21, in create_homepage
homepage = HomePage.objects.create(
File "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/db/models/query.py", line 447, in create
obj.save(force_insert=True, using=self.db)
File "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/db/models/base.py", line 753, in save
self.save_base(using=using, force_insert=force_insert,
File "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/db/models/base.py", line 789, in save_base
parent_inserted = self._save_parents(cls, using, update_fields)
File "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/db/models/base.py", line 818, in _save_parents
updated = self._save_table(
File "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/db/models/base.py", line 895, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/db/models/base.py", line 933, in _do_insert
return manager._insert(
File "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/db/models/query.py", line 1254, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1397, in execute_sq
l
cursor.execute(sql, params)
File "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wra
ppers
return executor(sql, params, many, context)
File "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/yogagarten/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 78, in execute
raise IntegrityError(*tuple(e.args))
django.db.utils.IntegrityError: (1048, "Column 'locale_id' cannot be null")
My settings.py
INSTALLED_APPS = [
'wagtail.locales',
]
LANGUAGE_CODE = 'de-DE'
LANGUAGES = [('de', 'German'),]
WAGTAILADMIN_PERMITTED_LANGUAGES = [('de', 'German'),]
TIME_ZONE = 'Europe/Berlin'
USE_I18N = True
USE_L10N = True
USE_TZ = True
Basic model example:
class HomePage(Page):
template = 'home/index.html'
ueberschrift = models.CharField(max_length=60, blank=False, null=True)
content_panels = ([FieldPanel('ueberschrift')])
class Meta:
verbose_name = 'Startseite'
I haven't created any page objects yet.
Can you give me some advice on how to fix this error? I understand that every page model comes with a locale field. But I don't know what to do with that info.
What I've tried:
redid everything with a new database
reset the language settings to default (en-us)
I tried the run_before command.
I checked this answer and followed this part of
the official documentation
I'm using wagtail 2.11 with django 3.1.3 and python 3.8.
Thanks so much!
You need to add the following line to the 0002_create_homepage.py migration file, inside class Migration:
run_before = [
('wagtailcore', '0053_locale_model'),
]
This change is necessary as a result of the new multi-language implementation in Wagtail 2.11. More details are here: https://docs.wagtail.io/en/stable/releases/2.11.html#run-before-declaration-needed-in-initial-homepage-migration
If you've already run ./manage.py migrate without this new line in place and received the locale_id IntegrityError, you'll need to drop and recreate the database and then re-run ./manage.py migrate, as the previous failure will have left the database in an inconsistent state. (Dropping and recreating the database will delete any existing data, but as this issue will only arise on a new deployment on a clean database anyway, that shouldn't be an issue.)

Django-parler unique_together constraint between translated and normal field

I want to make unique_together constraint with django-parler between translated field and normal field.
I'd like to do this:
class Part(TranslatableModel):
code = CharField('code')
translations = TranslatedFields(
name = CharField('name')
)
class Meta:
constraints = [
UniqueConstraint(
fields=['code', 'name'],
name='constraint_name',
)
]
but then I get error:
Traceback (most recent call last):
File "manage.py", line 14, in <module>
execute_from_command_line(sys.argv)
File "/home/lauri/työt/p/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/lauri/työt/p/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/lauri/työt/p/venv/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/lauri/työt/p/venv/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/home/lauri/työt/p/venv/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/lauri/työt/p/venv/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 234, in handle
fake_initial=fake_initial,
File "/home/lauri/työt/p/venv/lib/python3.6/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/lauri/työt/p/venv/lib/python3.6/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/lauri/työt/p/venv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/lauri/työt/p/venv/lib/python3.6/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/lauri/työt/p/venv/lib/python3.6/site-packages/django/db/migrations/operations/models.py", line 827, in database_forwards
schema_editor.add_constraint(model, self.constraint)
File "/home/lauri/työt/p/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 405, in add_constraint
self._remake_table(model)
File "/home/lauri/työt/p/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 279, in _remake_table
self.create_model(new_model)
File "/home/lauri/työt/p/venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 296, in create_model
constraints = [constraint.constraint_sql(model, self) for constraint in model._meta.constraints]
File "/home/lauri/työt/p/venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 296, in <listcomp>
constraints = [constraint.constraint_sql(model, self) for constraint in model._meta.constraints]
File "/home/lauri/työt/p/venv/lib/python3.6/site-packages/django/db/models/constraints.py", line 89, in constraint_sql
fields = [model._meta.get_field(field_name).column for field_name in self.fields]
File "/home/lauri/työt/p/venv/lib/python3.6/site-packages/django/db/models/constraints.py", line 89, in <listcomp>
fields = [model._meta.get_field(field_name).column for field_name in self.fields]
File "/home/lauri/työt/p/venv/lib/python3.6/site-packages/django/db/models/options.py", line 567, in get_field
raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: NewPart has no field named 'name'
I'm not very aware of insides of django-parler so I'm not able to move forward with this.
When you make migrations there will be two tables Part and PartTranslation. What you want is single translation for single post. So the following lines solves your problem
translations = TranslatedFields(
name = CharField('name'),
meta={'unique_together': [('name', 'language_code')]}
)
For uniqueness of Parts just make the code field unique.
code = CharField('code', unique=True)

How to remove a field in Django model - variation on answered question

Although a couple of similar question were posted, none of those questions are the same as my case.
I had a model that looked like this:
class Person(models.Model):
"""Definition of persons that will fulfill a role in a committee
or will be in a way associated with a committee as an administrator
"""
ClientId = models.ForeignKey('clients.Client', on_delete=models.CASCADE,
to_field='id')
PersNumber = models.PositiveIntegerField(null=False)
PersSurName = models.CharField(max_length=40, null=False)
PersNames = models.CharField(max_length=40, null=False)
I set the uniqueness of the record on ClientId and PersNumber.
I have created 3 records in the database.
Along the way I became convinced that I might as well use the auto generated id of the record as the person number (I am learning Django).
I removed the PersNumber from my model and ran makemigrations.
All is well until I ran migrate...
I get the following error:
django.core.exceptions.FieldDoesNotExist: Person has no field named 'PersNumber'
Any idee on how to get past this error
The full trace looks like this:
Operations to perform:
Apply all migrations: admin, auth, clients, contenttypes, komadm_conf, sessions
Running migrations:
Applying komadm_conf.0017_auto_20180830_1806...Traceback (most recent call last):
File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\db\models\options.py", line 564, in get_field
return self.fields_map[field_name]
KeyError: 'PersNumber'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 15, in
execute_from_command_line(sys.argv)
File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\core\management__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\core\management__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\core\management\base.py", line 353, in execute
output = self.handle(*args, **options)
File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\core\management\commands\migrate.py", line 203, in handle
fake_initial=fake_initial,
File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_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:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_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:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_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:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\db\migrations\operations\fields.py", line 150, in database_forwards
schema_editor.remove_field(from_model, from_model._meta.get_field(self.name))
File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\db\backends\sqlite3\schema.py", line 318, in remove_field
self._remake_table(model, delete_field=field)
File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\db\backends\sqlite3\schema.py", line 257, in _remake_table
self.create_model(temp_model)
File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\db\backends\base\schema.py", line 300, in create_model
columns = [model._meta.get_field(field).column for field in fields]
File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\db\backends\base\schema.py", line 300, in
columns = [model._meta.get_field(field).column for field in fields]
File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\db\models\options.py", line 566, in get_field
raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: Person has no field named 'PersNumber'
Giving the fact that nobody else answered i will(but i am learning too)..
did you check if this field still exist ? (in the shell for instance).
i'll recommand to manually delete the migration file associated and try again (makemigrations + migrate).
The migration file i am talking about can be found MyProject/Myapp/migrations/00xx_something.py
If it still does not work you can delete the table directly in your database, and the migration file associated.

I faced to django.core.exceptions.ValidationError: ["'' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]

data model is below:
class User(models.Model):
user_name = models.CharField(max_length=50)
location = models.CharField(max_length=5)
password = models.CharField(max_length=256,default='')
last_time = models.DateTimeField(null=True,default='')
def __str__(self):
return self.user_name
I just tried python manage.py migrate.
But, Django tells are...(I don't know what I make mistake.)
Operations to perform:
Synchronize unmigrated apps: messages, staticfiles
Apply all migrations: sessions, data, admin, books, auth, poll, contenttypes
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying data.0003_user_last_time...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 221, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/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/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/executor.py", line 147, in apply_migration
state = migration.apply(state, schema_editor)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/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/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
field,
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/sqlite3/schema.py", line 179, in add_field
self._remake_table(model, create_fields=[field])
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/sqlite3/schema.py", line 77, in _remake_table
self.effective_default(field)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 211, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 710, in get_db_prep_save
prepared=False)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1482, in get_db_prep_value
value = self.get_prep_value(value)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1461, in get_prep_value
value = super(DateTimeField, self).get_prep_value(value)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1317, in get_prep_value
return self.to_python(value)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1444, in to_python
params={'value': value},
django.core.exceptions.ValidationError: ["'' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]
By now you've probably already found out, but models.DateTimeField doesn't allow for empty strings. Instead, leave out the default attribute. Django will then set the default of the field to None. In addition, set the blank attribute to True as well, so that the field is not required.
last_time = models.DateTimeField(null=True, blank=True)
See Django Documentation on DateTimeField.

Resources