2

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_'

2 Answers 2

3

You can use meta options for model,

class ModelHere():
      class Meta:
          db_table = "tablenamehere"

Edit

If you want to add prefix to all of your tables including auth_user, auth_group, etc. Then you are looking for something like django-table-prefix. Just install and add some settings to settings file and you are done.

  1. Add 'table_prefix', to installed apps,
  2. Set the table prefix as DB_PREFIX = 'nifty_prefix'

Then run syncdb and the output will be,

Creating tables ...
Creating table nifty_prefix_auth_permission
Creating table nifty_prefix_auth_group_permissions
Creating table nifty_prefix_auth_group
Creating table nifty_prefix_auth_user_groups
Creating table nifty_prefix_auth_user_user_permissions
Creating table nifty_prefix_auth_user
Creating table nifty_prefix_django_content_type
Creating table nifty_prefix_django_session
Creating table nifty_prefix_django_site
Sign up to request clarification or add additional context in comments.

4 Comments

This one is good and i know it. But i need prefix for whole django tables, including auth_user, auth_group, etc.
i notice table_prefix, but it doesn't work for django 1.8.
@copycat You must mention all of your research and requirement in question itself. You didn't mentioned in question as you are using django 1.8
i'll remember it, for future
1

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;

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.