我遇到了django租户的问题。 我在我的创建函数上创建了一个随机的schema_name和domain_url,如下所示:
new_key = random_id()
request.data['schema_name'] = new_key
request.data['domain_url'] = new_key
def random_id(size=16, chars=string.ascii_uppercase):
return ''.join(random.choice(chars) for _ in range(size))
当我执行这段代码时,我得到一个Unable来创建django_migrations表(关系" django_migrations"已经存在)但是这只发生了为什么我尝试使用这个random_id函数,如果我设置一个静态值我完全没问题。我认为这可能是一个时间问题。有什么想法吗? 感谢
=== Running migrate for schema CNMHJLELEIQPUSPI
Operations to perform:
Apply all migrations: clients, oauth2_provider, locale, payables, auth, receivables, accounting, coop, guardian, users, contenttypes, budget
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial...Internal Server Error: /clients/
Traceback (most recent call last):
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
psycopg2.ProgrammingError: relation "django_migrations" already exists
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/django/db/migrations/recorder.py", line 57, in ensure_schema
editor.create_model(self.Migration)
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 284, in create_model
self.execute(sql, params or None)
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 110, in execute
cursor.execute(sql, params)
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "django_migrations" already exists
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/rest_framework/viewsets.py", line 87, in view
return self.dispatch(request, *args, **kwargs)
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/rest_framework/views.py", line 466, in dispatch
response = self.handle_exception(exc)
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/rest_framework/views.py", line 463, in dispatch
response = handler(request, *args, **kwargs)
File "/home/griffin/workspace/myapp/superapp/superapp/clients/views.py", line 159, in create
user_validated_data=user_serializer.validated_data
File "/home/griffin/workspace/myapp/superapp/superapp/clients/serializers.py", line 40, in create
on_trial=validated_data['on_trial']
File "/home/griffin/workspace/myapp/superapp/superapp/clients/models.py", line 24, in create
new_client.save()
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/tenant_schemas/models.py", line 52, in save
self.create_schema(check_if_exists=True, verbosity=verbosity)
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/tenant_schemas/models.py", line 99, in create_schema
verbosity=verbosity)
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/django/core/management/__init__.py", line 119, in call_command
return command.execute(*args, **defaults)
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/tenant_schemas/management/commands/migrate_schemas.py", line 39, in handle
self.run_migrations(self.schema_name, settings.TENANT_APPS)
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/tenant_schemas/management/commands/migrate_schemas.py", line 50, in run_migrations
command.execute(*self.args, **self.options)
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/django/db/migrations/executor.py", line 92, in migrate
self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-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 "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/django/db/migrations/executor.py", line 204, in apply_migration
self.recorder.record_applied(migration.app_label, migration.name)
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/django/db/migrations/recorder.py", line 72, in record_applied
self.ensure_schema()
File "/home/griffin/virtualenv/myapp/lib/python3.4/site-packages/django/db/migrations/recorder.py", line 59, in ensure_schema
raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (relation "django_migrations" already exists
这是一个API,我实际上发送了一个包含该信息的发布请求,并使用函数random_id创建了domain_url和schema_name。这只发生在我使用该函数时,而不是在我将该信息指定为字符串时。
答案 0 :(得分:0)
默认情况下,postgres会将每个大写架构名称转换为小写
CREATE SCHEMA Km9GDUY1L9vKexXd;
\dn
List of schemas
Name | Owner
------------------+--------
first | mo
km9gduy1l9vkexxd | mo
myschema | mo
public | mo
由于我们是从python代码创建模式,所以将ascii_uppercase更改为ascii_lowercase
def random_id(size=16, chars=string.ascii_lowercase):
return ''.join(random.choice(chars) for _ in range(size))