I am working on a college project for which I am supposed to use oracle database as the backend and I have chosen to work with django as the programming language.
I have successfully connected django with oracle 12c and also have cx_Oracle installed. But when i try to run the command
py manage.py migrate
the following errors show up.
Windows PowerShell Copyright (C) Microsoft Corporation. All rights
reserved.
PS C:\Users\Hp\Desktop\onlinepharma> py manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, testing Running migrations: Applying contenttypes.0001_initial...Traceback
(most recent call last): File
"C:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-
packages\django\db\backends\utils.py", line 83, in _execute
return self.cursor.execute(sql) File "C:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\oracle\base.py",
line 513, in execute
return self.cursor.execute(query, self._param_generator(params)) cx_Oracle.DatabaseError: ORA-00955: name is already used by an
existing object
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "manage.py", line 15, in
execute_from_command_line(sys.argv) File "C:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management__init__.py",
line 381, in execute_from_command_line
utility.execute() File "C:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management__init__.py",
line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py",
line 316, in run_from_argv
self.execute(*args, **cmd_options) File "C:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py",
line 353, in execute
output = self.handle(*args, **options) File "C:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py",
line 83, in wrapped
res = handle_func(*args, **kwargs) File "C:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\commands\migrate.py",
line 203, in handle
fake_initial=fake_initial, File "C:\Users\Hp\AppData\Local\Programs\Python\Python37\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:\Users\Hp\AppData\Local\Programs\Python\Python37\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:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\executor.py",
line 244, in apply_migration
state = migration.apply(state, schema_editor) File "C:\Users\Hp\AppData\Local\Programs\Python\Python37\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:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\operations\models.py",
line 91, in database_forwards
schema_editor.create_model(model) File "C:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\base\schema.py",
line 312, in create_model
self.execute(sql, params or None) File "C:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\base\schema.py",
line 133, in execute
cursor.execute(sql, params) File "C:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py",
line 100, in execute
return super().execute(sql, params) File "C:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py",
line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File
"C:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py",
line 77, in _execute_with_wrappers
return executor(sql, params, many, context) File "C:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py",
line 85, in _execute
return self.cursor.execute(sql, params) File "C:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\utils.py",
line 89, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value File "C:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py",
line 83, in _execute
return self.cursor.execute(sql) File "C:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\oracle\base.py",
line 513, in execute
return self.cursor.execute(query, self._param_generator(params)) django.db.utils.DatabaseError: ORA-00955: name is already used by an
existing object
Here's my model.py file
from django.db import models
# Create your models here.
class yoman(models.Model):
text=models.CharField(max_length=200)
But I have checked using sql command line and no such db table or view exists.
p.s: The makemigrations command works properly and the migratiosn file has also been created.
You can print out the sql query if you edit the following file:
"C:\Users\Hp\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\oracle\base.py
On line 513, before the execute function is called add:
print(f'query')
Rerun the migration and you can see the sql that is failing to run. Either remove or rename the table/object in the db.
Related
What im doing is i clone my prod database to sandbox. and I'm getting error while migrate Cause of previously I made charfield to datetime field. And then datetime field to Charfield after few migrations.
So how do I solve below error:
Error :
python manage.py migrate
System check identified some issues:
WARNINGS:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, pro_auth, service_list, sessions, token_blacklist
Running migrations:
Applying pro_auth.0002_alter_userorder_order_booking_time...Traceback (most recent call last):
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.InvalidDatetimeFormat: invalid input syntax for type timestamp with time zone: "2021-10-10 10.00"
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
output = self.handle(*args, **options)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/core/management/base.py", line 89, in wrapped
res = handle_func(*args, **kwargs)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 244, in handle
post_migrate_state = executor.migrate(
File "/root/sandbox/sandbox_venv/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 "/root/sandbox/sandbox_venv/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 "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/migrations/migration.py", line 126, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/migrations/operations/fields.py", line 244, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 608, in alter_field
self._alter_field(model, old_field, new_field, old_type, new_type,
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/postgresql/schema.py", line 196, in _alter_field
super()._alter_field(
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 765, in _alter_field
self.execute(
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 145, in execute
cursor.execute(sql, params)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/root/sandbox/sandbox_venv/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 "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.DataError: invalid input syntax for type timestamp with time zone: "2021-10-10 10.00"
And Here is Migration file 0002_alter_userorder_order_booking_time:
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('pro_auth', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='userorder',
name='order_booking_time',
field=models.DateTimeField(default=django.utils.timezone.now),
),
]
I think, you need fake migration.
Try this
./manange.py migrate --fake <app_name_of_userorder_is_inclueded> <last_migration_number_which_synced_current_order_booking_time>
This make skip applying alter table.
You're not passing a valid DateTimeFormat. I think you should write
field=models.DateTimeField(default=django.utils.timezone.now()),
based on what is shown here: Django default=timezone.now() saves records using "old" time
I'm setting up a database in django, while trying to flush the database i get the following error:
You have requested a flush of the database.
This will IRREVERSIBLY DESTROY all data currently in the "/home/luc/projects/PySorge/API/db.sqlite3" database,
and return each table to an empty state.
Are you sure you want to do this?
Type yes to continue, or no to cancel: yes
Traceback (most recent call last):
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 413, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: django_content_type.name
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/luc/projects/PySorge/API/manage.py", line 22, in <module>
main()
File "/home/luc/projects/PySorge/API/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/commands/flush.py", line 80, in handle
emit_post_migrate_signal(verbosity, interactive, database)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/sql.py", line 48, in emit_post_migrate_signal
models.signals.post_migrate.send(
File "/home/luc/.local/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 177, in send
return [
File "/home/luc/.local/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 178, in <listcomp>
(receiver, receiver(signal=self, sender=sender, **named))
File "/home/luc/.local/lib/python3.9/site-packages/django/contrib/auth/management/__init__.py", line 42, in create_permissions
create_contenttypes(app_config, verbosity=verbosity, interactive=interactive, using=using, apps=apps, **kwargs)
File "/home/luc/.local/lib/python3.9/site-packages/django/contrib/contenttypes/management/__init__.py", line 132, in create_contenttypes
ContentType.objects.using(using).bulk_create(cts)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/models/query.py", line 506, in bulk_create
returned_columns = self._batched_insert(
File "/home/luc/.local/lib/python3.9/site-packages/django/db/models/query.py", line 1277, in _batched_insert
self._insert(item, fields=fields, using=self.db, ignore_conflicts=ignore_conflicts)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/models/query.py", line 1254, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1397, in execute_sql
cursor.execute(sql, params)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/home/luc/.local/lib/python3.9/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/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 413, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: django_content_type.name
While trying to migrate, the following error occured:
Operations to perform:
Apply all migrations: admin, api, auth, contenttypes, sessions
Running migrations:
Applying api.0001_initial...Traceback (most recent call last):
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 82, in _execute
return self.cursor.execute(sql)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 411, in execute
return Database.Cursor.execute(self, query)
sqlite3.OperationalError: duplicate column name: ID
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/luc/projects/PySorge/API/manage.py", line 22, in <module>
main()
File "/home/luc/projects/PySorge/API/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 243, in handle
post_migrate_state = executor.migrate(
File "/home/luc/.local/lib/python3.9/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/luc/.local/lib/python3.9/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/luc/.local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/luc/.local/lib/python3.9/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/luc/.local/lib/python3.9/site-packages/django/db/migrations/operations/models.py", line 92, in database_forwards
schema_editor.create_model(model)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 324, in create_model
self.execute(sql, params or None)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 142, in execute
cursor.execute(sql, params)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/home/luc/.local/lib/python3.9/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/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 82, in _execute
return self.cursor.execute(sql)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 411, in execute
return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: duplicate column name: ID
I'm using django version 3.1.6. I'm not really sure how to fix this as I'm not very familiar with databases. Seems like the error is caused by a duplicate field id - my current database setup does have a field named ID.
My recommendation, although certainly not the best way forward, would be to delete the database (delete the actual sqlite3 file in your project) then delete all of your previous migrations, and then try making migrations again.
I had a very old Django site (still had South migrations in tree!) that had been ticking along on a SQLite database forever. At some point Django removed the name column from the django_content_types table. The model uses a property now based on the reflected model class. I've no idea why this hadn't be caught by a Django migration, but I've been known to fake migrations in the blood mist of getting something to run, no-matter-what.
I just dropped the column in ./manage.py dbshell:
ALTER TABLE django_content_type DROP COLUMN name;
And migrations worked again. No ill effects felt. All my old contenttypes are in there, everything still works.
python manage.py reset_db
This will delete all tables. It also helps to delete all migration files in the project if you want a complete wipeout
I've recently upgraded form sqlite to postgresql for my django project.
In the past, whenever I've made a change to a model, I simply ran makemigrations and then the migrate command. No issues.
Now, with posgresql, when I make the INITIAL makemigrations/migrate, it works fine as per usual, but if I make a change to a model and then run makemigrations and then migrate, on the 'migrate' command, the system tries to apply the initial migration again and returns an error saying the table already exist (which it does).
How can I run migrate without the system trying to remigrate migrations that have already been migrated?
Thanks!
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/jimmy/lib/python3.6/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/jimmy/lib/python3.6/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/jimmy/lib/python3.6/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/jimmy/lib/python3.6/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/home/jimmy/lib/python3.6/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/jimmy/lib/python3.6/django/core/management/commands/migrate.py", line 233, in handle
fake_initial=fake_initial,
File "/home/jimmy/lib/python3.6/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/jimmy/lib/python3.6/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/jimmy/lib/python3.6/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/jimmy/lib/python3.6/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/jimmy/lib/python3.6/django/db/migrations/operations/models.py", line 92, in database_forwards
schema_editor.create_model(model)
File "/home/jimmy/lib/python3.6/django/db/backends/base/schema.py", line 324, in create_model
self.execute(sql, params or None)
File "/home/jimmy/lib/python3.6/django/db/backends/base/schema.py", line 142, in execute
cursor.execute(sql, params)
File "/home/jimmy/lib/python3.6/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/jimmy/lib/python3.6/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/jimmy/lib/python3.6/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/home/jimmy/lib/python3.6/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/jimmy/lib/python3.6/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "profiles_accentcolor" already exists
Looks like i have to run python manage.py migrate --fake-initial to ignore initial migrations.
I am trying to implement the django rest framework in my current project. The api engine is working with no security. I am trying to follow the steps given in the authentication page of django rest framework to add the authentication.
When I am adding the following section to the settings.py, I don't get any error:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication',
]
}
but when I am adding 'rest_framework.authtoken' to the INSTALLED_APPS, and run the server,
I get the follwing message:
You have 2 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): authtoken.
Run 'python manage.py migrate' to apply them.
Then I tried running:
python manage.py migrate
and I get the following errors:
(raiotic-venv) username#username-VirtualBox:~/Servers/Repositories/raiotic-venv/raiotic$ python manage.py migrate
Operations to perform:
Apply all migrations: MainApp, admin, auth, authtoken, contenttypes, sessions
Running migrations:
Applying authtoken.0001_initial...Traceback (most recent call last):
File "/home/username/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute
return self.cursor.execute(sql)
File "/home/username/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 381, in execute
return Database.Cursor.execute(self, query)
sqlite3.OperationalError: attempt to write a readonly database
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/username/venv/lib/python3.6/site-packages/django/core/management/init.py", line 381, in execute_from_command_line
utility.execute()
File "/home/username/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/username/venv/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/username/venv/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/home/username/venv/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/username/venv/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 234, in handle
fake_initial=fake_initial,
File "/home/username/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/username/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/username/venv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/username/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/username/venv/lib/python3.6/site-packages/django/db/migrations/operations/models.py", line 92, in database_forwards
schema_editor.create_model(model)
File "/home/username/venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 307, in create_model
self.execute(sql, params or None)
File "/home/username/venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 137, in execute
cursor.execute(sql, params)
File "/home/username/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "/home/username/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/username/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/username/venvlib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/username/venvlib/python3.6/site-packages/django/db/utils.py", line 89, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/username/venvlib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute
return self.cursor.execute(sql)
File "/home/username/venvlib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 381, in execute
return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: attempt to write a readonly database
try changing the permissions to the db.sqlite3 file it might be that the mode is read only for the user you are running as having no permissions to write to the file.
do
ls -al db.sqlite3
chmod a+w db.sqlite3
python manage.py migrate
I have deleted a table from sqllite3 shell and then when I try creating it again by running python manage.py migrate/python manage.py makemigrations table is not created and showing below error. How to re-create the table and make my db and the API is insync. I tried with the python manage.py dbsync also but not worked.
Operations to perform:
Apply all migrations: admin, auth, cgkapp, contenttypes, sessions
Running migrations:
Applying cgkapp.0002_delete_questions...Traceback (most recent call last):
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: cgkapp_questions
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 "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
output = self.handle(*args, **options)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 200, in handle
fake_initial=fake_initial,
File "/home/shrivatsa555/.virtualenvs/django20/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/shrivatsa555/.virtualenvs/django20/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/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/migrations/migration.py", line 122, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/migrations/operations/models.py", line 246, in database_forwa
rds
schema_editor.delete_model(model)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 287, in delete_model
super().delete_model(model)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 317, in delete_model
"table": self.quote_name(model._meta.db_table),
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 117, in execute
cursor.execute(sql, params)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: cgkapp_questions
Django migrate command try to make all migrations (they hold in migrations directory) which are not done yet.
In here you have "0002_delete_questions" migration file in your migrations directory and when you run the migrate command this migration try to delete questions table from your database but this table is already deleted. So you can handle this in two way
Delete this migration file from migrations directory in your project
run this command python manage.py migrate --fake cgkapp 0002_delete_questions (here 'cgkapp' is your app name and '0002_delete_questions' is your migration file name)