Ben*_*ari 2 python sqlite django postgresql
我想从sqlite迁移到postgresql数据库:
我安装了 postgresql 并在其 shell 上创建数据库,然后配置我的 django 设置如下:
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'dbname',
'USER': 'username',
'PASSWORD': 'dbpass',
'HOST': 'localhost',
'PORT': '',
}
Run Code Online (Sandbox Code Playgroud)
或者:
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'USER': 'username',
'PASSWORD': 'dbpass',
'HOST': 'localhost',
'PORT': '',
}
Run Code Online (Sandbox Code Playgroud)
但是python manage.py migrate我什么时候遇到这个错误:
ValueError: invalid literal for int() with base 10: '0x000
Run Code Online (Sandbox Code Playgroud)
完整回溯:
Operations to perform:
Apply all migrations: delta_device, admin, menu, sessions, datapipeline, datacollector, siemens_s7, contenttypes, auth, settings
Running migrations:
Rendering model states... DONE
Applying delta_device.0002_auto_20171210_1631...Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 342, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 92, in migrate
self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 198, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 123, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 482, in alter_field
old_db_params, new_db_params, strict)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 565, in _alter_field
new_default = self.effective_default(new_field)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 210, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 728, in get_db_prep_save
prepared=False)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 720, in get_db_prep_value
value = self.get_prep_value(value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1853, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: '0x000
Run Code Online (Sandbox Code Playgroud)
怎么了?!
我决定从头开始解释这些说明:
在您的计算机上安装 Postgres。
sudo apt-get install libpq-dev python-devPostgres 依赖项以与 Django 完美配合。sudo apt-get install postgresql postgresql-contrib命令安装 Postgres。使用命令访问 Postgres sudo su - postgres。
创建一个新数据库。createdb <dbname>
创建数据库用户(带密码)。createuser -P <username>
使用命令访问 shell psql。
使用命令授予此新用户对新数据库的访问权限GRANT ALL PRIVILEGES ON DATABASE <dbname> TO <username>;。
转储现有数据。python3 manage.py dumpdata > datadump.json
安装 Postgres 包。pip install psycopg2
将 settings.py 配置更改为以下内容:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '<dbname>',
'USER': '<username>',
'PASSWORD': '<password>',
'HOST': 'localhost',
'PORT': '',
}
}
Run Code Online (Sandbox Code Playgroud)
确保您可以连接到 Postgres 数据库。python3 manage.py migrate --run-syncdb
在 Django shell 上运行此命令以排除内容类型数据。
python3 manage.py shell
>>> from django.contrib.contenttypes.models import ContentType
>>> ContentType.objects.all().delete()
>>> quit()
Run Code Online (Sandbox Code Playgroud)
python3 manage.py loaddata datadump.json| 归档时间: |
|
| 查看次数: |
2437 次 |
| 最近记录: |