Chrysolites Tech Academy

Photo Social App with FlutterFlow and Supabase · Course book

Chapter 23: Follows

Pairs with Lesson 23: Following Other Users

Following turns a collection of strangers' photos into a personal network. When you follow someone, their posts become part of your world, and the numbers on each profile, followers and following, show how connected each person is. This chapter builds following in FlutterFlow and Supabase using a core database idea, the intermediate table. It designs the follow relationship, builds the follow button on other users' profiles, prevents duplicate follows at the database level, keeps the follower and following counts correct, and tests it all, with the size of the follow network worked out by hand.

The follow relationship

A follow connects two users, and it has a direction: if Maya follows Alex, Alex has not necessarily followed Maya. Any user can follow many others and be followed by many, so this is a many-to-many relationship between a table and itself. As with likes, it is stored in an intermediate table, here called user_follower, whose rows each record one follow.

ColumnTypeMeaningRule
idint8Identifies the rowPrimary key
follower_iduuidThe person who followsForeign key to users; required
following_iduuidThe person being followedForeign key to users; required
created_attimestamptzWhen the follow beganDefault now()
(follower_id, following_id)–One follow per pairUnique together; follower and following must differ

Two columns point at the same users table, so their names must make the direction unmistakable; follower_id and following_id read clearly in every query. "Who does Maya follow?" selects rows where follower_id is Maya; "who follows Maya?" selects rows where following_id is Maya. An index on each column keeps both questions fast. Cascading deletes from users mean that deleting an account removes all of its follows in both directions.

Row level security policies let signed-in users read follows, which are public in a social app, insert only rows where follower_id is their own id, and delete only their own follows, so nobody can make someone else follow or unfollow anybody.

The same table answers richer questions. Mutual follows, people who follow each other, are pairs where a row exists in each direction, found by joining the table to itself; many apps treat mutual followers as friends, which suits a friends-only feed or limiting chat to people who know each other. A following feed shows posts whose author is among the people the viewer follows, a query that joins posts with user_follower on the author and filters by the viewer as follower. And suggestions, "people you may know", can be found among the people followed by those you follow, excluding anyone you already follow. All of these come from the one simple table, which is why it pays to design it carefully.

Privacy settings would extend the design rather than replace it. A private account could require approval, with a status column holding "requested" or "accepted", and only accepted follows counting or unlocking the account's posts. Blocking would add a separate table of blocks consulted by the policies, so that a blocked person cannot follow, see posts or comment. Neither is part of this series, but knowing where they would fit confirms that the table is a sound foundation.

How big is the follow network?

With n users, there are n × (n − 1) possible directed follows, because each user could follow every other user. In practice people follow a small fraction, but estimating helps plan for growth.

Worked example. With 1,000 users there are 1,000 × 999 = 999,000 possible follows. If each user follows 150 people on average, the table holds 1,000 × 150 = 150,000 rows, about 15 percent of the possible pairs. With 100,000 users each following 150 people, it holds 15 million rows; at roughly 72 bytes per row, about 1.1 GB, while the possible pairs number almost 10 billion, so the network is extremely sparse: only 0.15 percent of possible follows exist. That sparsity is why storing each follow as a row works, where storing a full grid of every pair would be impossible.
1 million10 billion 150,00015 million possible follows actual follows (150 each) 1051061081010 1,00010,000100,000 users (log scale)follows (log scale)
Figure 23.1 — Possible follows grow with the square of the number of users; actual follows grow in proportion to users, so the network becomes ever sparser.

The follow page and button

Other users' profiles show their picture, name, bio, statistics and posts, and a Follow button. Tapping it inserts a row into user_follower with the viewer as follower and the profile's owner as following, and changes the button to Following, styled more quietly, to show the current state. The profile page receives the user to show as a page parameter, so the same page works for anyone, opened from a post's header, a comment, search results or a follower list.

The button must show the right state as soon as the page opens. Like the heart on posts, that means knowing whether the viewer already follows this person. Loading the ids of everyone the viewer follows once, after sign-in, into a list in app state lets every Follow button check instantly, and updating the list on each follow keeps all screens consistent, including the friends feed and suggestion lists added later. On the viewer's own profile, the button is replaced by "Edit profile", decided by comparing the profile's id with the signed-in user's id.

The button's states deserve a little design attention. "Follow" is the primary action, so it uses the theme's strong accent colour; "Following" is a state rather than an invitation, so it uses a quieter outlined or grey style. Many apps also show "Follow back" when the profile's owner already follows the viewer, a small prompt that encourages mutual connections, which the app can detect by checking whether a row exists in the other direction. Whatever the label, the button should be at least 44 points tall and wide enough for its longest label, so that it does not change size and shift the layout when its text changes.

Preventing duplicate follows

A quick double tap, a slow network or a bug could try to insert the same follow twice, which would inflate counts and complicate unfollowing. The unique constraint on the pair of columns makes a duplicate impossible: the second insert is rejected by the database. The app can also check the follow list before inserting and disable the button while a request is in flight, but the constraint is the real guarantee.

A check constraint requiring follower_id and following_id to differ stops anyone following themselves, which the interface should prevent anyway by never showing a Follow button on the viewer's own profile. Rules like these belong in the database because they must hold no matter which app version, script or person writes to the table.

Follower and following counts

Every profile shows two numbers: followers, how many rows have this user as following_id, and following, how many have this user as follower_id. Counting on each visit works for small apps, but as with likes, stored counts kept by triggers are cheaper and stay correct automatically. Two columns on the users table, follower_count and following_count, are updated by a trigger on user_follower: when a follow is inserted, the follower's following count rises by one and the followed user's follower count rises by one; the next part adds the matching decrease when a follow is deleted.

The profile then reads both numbers directly, formatted compactly for large values. Immediately after tapping Follow, the visible follower count can be adjusted by one on the phone, so the change feels instant, and the stored count takes over on the next refresh. Tapping either number can open a list of the people behind it, built from the same table with the account row component from the search page.

The trigger function follows the same pattern as the like count trigger: it runs after each insert on user_follower, uses NEW.follower_id and NEW.following_id to find the two users, and performs both updates in the same transaction as the follow, so the follow and the counts always change together. Running with the owner's permissions lets it update another user's follower count, which ordinary users are not allowed to change directly.

Testing

Testing needs two accounts. From the first, open the second's profile and follow: the button should change, the second account's follower count should rise by one and the first account's following count should rise by one, as the Supabase table editor will confirm. Tap Follow twice quickly and check that only one row exists. Check that the button shows "Following" after closing and reopening the app, and that your own profile never shows a Follow button, however it is reached. The next part completes the picture with unfollowing and its matching trigger.

Practice tasks

  1. How many possible directed follows are there among 5,000 users? If each follows 80 people, what fraction of them exist?
  2. Write the filters for "who does Maya follow?" and "who follows Maya?".
  3. Explain the two constraints that keep the follow table clean.
  4. Which two counts does one follow change, and on whose rows?
  5. Why is the list of followed ids kept in app state?
  6. Describe a test that proves duplicate follows are impossible.

Install Chrysolites Tech Academy

Android / Chrome / Edge: open the browser menu and choose Install app or Add to Home screen.

iPhone / iPad (Safari): tap the Share button, then Add to Home Screen.