Django数据库前缀 [英] Django Database Prefix
问题描述
我需要为两个不同的应用程序合并两个数据库。如何为所有Django表添加前缀以避免任何冲突?
I need to merge two databases for two different apps. How can add prefix to all Django tables to avoid any conflict?
例如,选项应如下所示:
For example, option should look like:
DB_PREFIX = 'my_prefix_'
推荐答案
替代所有名称的前缀是将两个DB中的一个放入不同的模式
(多个模式可以在同一个数据库中共存,即使对象具有相同的名称),这也将处理表之外的对象,如索引,视图,功能,...
An alternative to prefixing all the names is to put one of the two DBs into a different schema (multiple schemas can coexist in the same database, even if the objects have the same names) This will also take care of objects other than tables, such as indexes, views, functions, ...
所以在其中一个数据库中,只要执行
So on one of the databases, just do
ALTER SCHEMA public RENAME TO myname;
之后,你可以转储它( pg_dump -n myname
只转储一个模式),并将其导入到其他数据库中,而不会发生冲突。
After that, you can dump it (pg_dump -n myname
to dump only one schema), and import it into the other database, without the chance of collisions.
您引用新表中的表或其他对象模式由 myname.tablname
或通过设置search_path(这可以在每个用户的基础上,例如通过 ALTER USER SET search_path = myschema,pg_catalog ;
)
You refer to tables or other objects in the new schema by myname.tablname
or by setting the search_path (this can be done on a per-user basis eg via ALTER USER SET search_path = myschema, pg_catalog;
)
注意:框架和客户端可能没有问题>模式感知,因此您可能需要进行一些额外的调整。因人而异。
Note: there may be a problem with frameworks and clients not being schema-aware, so you might need some additional tweaking. YMMV.
http://www.postgresql.org/docs/9.4/static/sql-alterschema.html
这篇关于Django数据库前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!