Using Multiple Databases
Each Anvil app can have multiple databases. You might want different deployment environments to use different databases – for example, so you can test your app without affecting production data. You can choose which database each environment uses. The tables in this database will be available via
anvil.tables.app_tables in this environment.
Adding an additional database
To add an additional database to your Anvil app, open “Data” in the Sidebar Menu and click “+ Add New Database”.
This will create a new database in your app with copies of the data tables from the default database. The data tables in the new database won’t contain any data.
Switching between databases in the editor
To change which database your app uses when it runs in the Editor, click on the three dots next to the
+ Add Table button of your desired database.
Use for development. The colour of the star, next to the database name, will change to denote which database the Editor is currently using.
Assigning databases to environments
To use your new database in an environment, click the Publish button in the top right of the editor:
This will open the Environments dialog:
Click “Advanced settings” and you will see a database section where you can select which database the environment should use.
A database schema contains information on the structure of a database i.e. what tables and columns a database should have. Each Anvil app has a database schema and when you load an app in the editor, Anvil checks to make sure the database the app is using matches the app’s database schema.
With multiple databases, the databases can become out of sync - for example, you might have added a new table to your development database.
The next time you begin using a database that is out of sync with the app’s schema, you will be prompted to resolve the schema mismatch.
Resolve button in the prompt will open the database schema window which will give you two options in large blue buttons. The options are, either the app’s source code schema is correct or the database schema is correct.
The app’s source code schema is correct
The source code is correctyou are carrying out a destructive action. If a table or column has been deleted in the schema, it will delete it from the database along with the data that table or column contained.
The source code is correct will update the database structure to match the app’s database schema. This includes adding and deleting tables and columns.
The database schema is correct
database is correct will update the app’s database schema to match the database structure.
Do you still have questions?
Our Community Forum is full of helpful information and Anvil experts.