Chapter 1:
Create your database tables

Let’s start by creating the Data Tables to store your news articles.

Step 1: Create an app

Log in to Anvil and click ‘New Blank App’. Choose the Material Design theme.

Location of the Create App button

First, name the app. Click on the name at the top of the screen and give it a name.

Rename your app by clicking on the title

Step 2: Add the Data Tables service

We’ll use Data Tables to store our news articles. Data Tables are an out-of-the-box option for data storage in Anvil, and they’re backed by PostgreSQL.

In the panel on the left (the App Browser), click the ‘+’ next to ‘Services’, then click on Data Tables.

Adding new service

Step 3: Create an 'articles' table

Now set up your ‘articles’ table, with the following columns:

  • title (Text column)
  • content (Text column)
  • image (Media column)
  • created (Date and Time column)
  • updated (Date and Time column)

Here’s how to do that:

  1. Click ‘Add a table’ in the light blue box at the top, then ‘Create new table…’. You’ll be prompted to give your table a name - let’s call it ‘articles’.

  2. Next, add a column for the article title, by clicking ‘+ New Column’ and choosing ‘Add text column…’. Call this column ‘title’.

  3. Keep adding columns until your Data Table has the structure we described above. Column titles are case sensitive, so let’s stick to lower case.

Creating the 'Articles' data table

Your Data Table should look something like this:

Database schema

Step 4: Create a Categories table

We’re also going to put each article into a ‘category’. These categories are fixed, so we’ll create a second table called ‘categories’ to store the categories, and link the ‘articles’ table to the categories table.

Create your ‘categories’ table, with a single column:

  • name (Text column)

Add these categories to the table:

  • entertainment
  • business
  • travel
  • sport
Creating the categories table

Finally, create a column in your ‘Articles’ table to store a category for each news article. Click the ‘+’ to add a new column, then follow ‘Link to table…’, ‘categories’, and select ‘Single Row’. Call this ‘category’. This creates a link to the ‘categories’ table you just created.

Creating the categories table

Your Data Tables are set up and ready to use. Nice work!

In Chapter 2, we’ll build a User Interface to display the articles.

Chapter complete

Congratulations, you've completed this chapter!