Original: https://codingcat.dev/post/firebase-2025-01-dataconnect-relationships
How Relationships Work in Data Connectmarks an important milestone for full-stack web and AI engineers building on Firebase and Google Cloud. Following the official announcement by Andrea Wu on theFirebase Blog(2025-01-28), this hands-on CodingCat.dev implementation guide breaks down how to adopt these capabilities in modern TypeScript, Next.js, SvelteKit, React, and Angular applications. Note: Firebase Data Connect has been renamed to SQL Connect. Learn more in the SQL Connect announcement. One of the major benefits of using a SQL database is that relationships between data are clearly defined and organized. Firebase Data Connect supports relationships in schemas, queries, and mutations, and this blog
Why How Relationships Work in Data Connect Matters for Full-Stack Web Developers
When evaluating every new Firebase release for our production web apps and Google Developer Expert (GDE) architecture reviews, the first question is always how it improves developer velocity, runtime security, and end-user latency in the browser and on the server.Note: Firebase Data Connect has been renamed to SQL Connect. Learn more in the SQL Connect announcement.
One of the major benefits of using a SQL database is that relationships between data are clearly defined and organized. Firebase Data Connect supports relationships in schemas, queries, and mutations, and this blog post will dive into setting up your schema to reflect relationships. We’ll assume that you are familiar withschemas, and you can read the Data Connectdocumentationif you need a refresher.
Relationships in relational databases
In relational databases, each table has a primary key to identify each record in the table, and these primary keys can be used in other tables as a foreign key to refer back to the record with that primary key. This creates a relationship between the two tables. There are one-to-one, one-to-many, and many-to-many relationships, and we’ll go over each one and how to create such relationships in Data Connect.
One-to-one relationships
In one-to-one relationships, one record in a table is associated with exactly one record in another table. For example, a “Movie” table that holds information about the movie such as title and genre can have a one-to-one relationship with a “Movie Metadata” table that keeps track of additional information such as the director, release year, and rating for each movie. A movie can have one metadata record, and each metadata is for one movie.
To define the relationship, theMovietable has a primary key that’s used in theMovieMetadatatable. See this below where theMovietable withmovieIdas the primary key and aMovieMetadatatable hasmovieMetadataIdas its primary key. In the latter table,movieIdis a foreign key, used to refer back to specific records in theMovietable.
With Data Connect, theMovietable looks like this:
type
id: UUID! @default(expr: "uuidV4()")
title: String!
genre: String
}
You can leave theidfield out, because Data Connect will automatically generate a primary key for you. The generated fields will be inimplicit.gql. The code above can be simplified to:
type
title: String!
genre: String
}
If you openimplicit.gql, there will be anidfield in the extendedMovieclass:
extend
id: UUID! @default(expr: "uuidV4()") @fdc_generated(from: "Movie",
purpose: IMPLICIT_KEY_FIELD)
}
Next, let’s see what theMovieMetadatatable without the foreign key fromMovielooks like:
type
director: String
rating: Float
releaseYear: Int
}
Again, anidprimary key gets automatically created and put intoimplicit.gql:
extend
id: UUID! @default(expr: "uuidV4()") @fdc_generated(from: "MovieMetadata", purpose: IMPLICIT_KEY_FIELD)
}
To add the foreign key with a one-to-one relationship, use@unique, which creates a field in the current table (MovieMetadata) and is a reference that holds the primary key of the referenced type:
type
movie: Movie! @unique
# movieId: UUID <-
director
rating
releaseYear
}
As a result, themovieIdfrom theMovietable will be included in this table, and themovieIdwill match the one in theMovietable.
One-to-many relationships
In one-to-many relationships, one record in a table is associated with many records in another table. For example, let’s say there are users for our app, and each user can have one favorite movie while one movie can have many users for which it’s their favorite.
With Data Connect, theUsertable looks like this:
type
id: String!
username: String!
}
TheFavoriteMovietable has this information:
type
# User to FavoriteMovie is 1:1, as one user can have only one favorite movie
user: User! @unique
# userId: UUID <-
#
movie
#
}
A combination of keys becomes the record’s unique identifier, the composite key. In this case for each record, the combination of user and movie needs to be unique, hencekey: ["user", "movie"].
We reuse@uniqueto signal that all users should be unique in thisFavoriteMovietable, since each user has one favorite movie. The movie field, however, does not need to be unique because one movie can be many users’ favorite movie (a many-to-many relationship, which gets covered in the next section).
@refforMovieis similar to @unique, in that it creates a field in the current table (FavoriteMovie) to reference the primary key of the referenced type. In this case,@ref(fields: "movieId", references: "id")is implied:
- fields: “movieId” is the name of the foreign key received from the
Movietable - references: “id” means the
movieIdfield is going to be the id from theMovietable
Writing@refforMovieis optional, as it’s implied, so the formerFavoriteMovietable’s code can be simplified to:
type
user: User! @unique
# userId: UUID <-
movie
#
}
Whenever an implicit reference is made,implicit.gqlincludes the type:
extend
userId: String! @fdc_generated(from: "FavoriteMovie.user", purpose: IMPLICIT_REF_FIELD)
movieId: UUID! @fdc_generated(from: "FavoriteMovie.movie", purpose: IMPLICIT_REF_FIELD)
}
There is noidfield since the combination ofuserIdandmovieIdis the composite key for each record.
Many-to-many relationships
In many-to-many relationships, many records in a table are associated with many records in another table, and typically, a join table is used to store these relations. An example of this is an actor being in many movies, and a movie having many actors. The table can hold additional information like whether the actor is a main or supporting actor for a movie.
With Data Connect, theActortable looks like this:
type
name: String!
}
We then create a join table betweenActorandMovieActorsto hold the many-to-many relationships information:
type
movie: Movie!
# movieId: UUID! <-
actor
#
role
}
Again, we declare a composite key withkey: ["movie", "actor"], and there can be the same movie repeated multiple times in the table, same with actors. For each movie-actor pair, the table will say whether the actor is a main or supporting actor for that movie.
With the implied references,implicit.gqlwill extend theMovieActorclass to include:
extend
movieId: UUID! @fdc_generated(from: "MovieActor.movie", purpose: IMPLICIT_REF_FIELD)
actorId: UUID! @fdc_generated(from: "MovieActor.actor", purpose: IMPLICIT_REF_FIELD)
}
Try it out!
Relationships are confusing and difficult (and not just the real-life kind), and hopefully this blog post clears up some of the muddy waters around how to create relationships in Data Connect. If you need more information about Data Connect, check out thedocumentation, and you can also check out other deep-divearticles. As it’s a new product, we’d love to hear your opinion and anyfeature requestsyou have as we’re hard at work improving and adding more features to the product. If you haven’t already, give Data Connect a try today - we can’t wait to see what you build with it!
Production Web Architecture & Security Checklist
Whether you are deploying onFirebase App Hosting, Cloudflare Workers, or Vercel, shipping SQL features to production requires defense-in-depth across three layers:
- Enforce Firebase App Check & Attestation:Protect your web endpoints, Cloud Functions, Data Connect GraphQL resolvers, and Firebase AI Logic calls against unauthorized replay or billing abuse usingFirebase App Check with reCAPTCHA Enterprise.
- Validate Locally with the Firebase Emulator Suite:Run deterministic integration tests against local Firestore, Auth, Functions, and Data Connect PostgreSQL emulators before promoting schema or rule changes to production.
- Keep Server & Client Boundaries Type-Safe:Use the modular Firebase Web SDK and generated TypeScript bindings so SSR frameworks like Next.js, SvelteKit, and Angular serialize state cleanly without hydration flickers.
Watch Our Related Firebase Video Tutorial on YouTube
Want to see how we architect and debug Firebase web applications live? Watch our related CodingCat.dev sessionFirebase SQL with Data Connectbelow, and make sure toSubscribe to CodingCat.dev on YouTubefor weekly full-stack web and AI deep dives:
Resources, Official Docs & Related CodingCat.dev Guides
- Primary Release Source:How Relationships Work in Data Connect on the Official Firebase Blog(published 2025-01-28 by Andrea Wu)
- Official Firebase Documentation:Firebase Developer Docs & Web SDK Reference
- Related on CodingCat.dev:Data Connect: All About Schemas
- Related on CodingCat.dev:Firebase SQL with Data Connect
- Related on CodingCat.dev:Build a Custom No-Code App in 90 Minutes
Frequently Asked Questions (FAQ)
What are the key capabilities introduced in How Relationships Work in Data Connect?
How Relationships Work in Data Connect (announced 2025-01-28) enhances the Firebase platform for Firebase, Google Cloud, Web Dev, SQL workloads. Note: Firebase Data Connect has been renamed to SQL Connect. Learn more in the SQL Connect announcement. Developers can review the official release notes on theFirebase Blogalongside the implementation patterns in this guide.
How do web developers use How Relationships Work in Data Connect with Next.js, SvelteKit, React, or Angular?
Web developers can integrate these features using the modular Firebase JavaScript/TypeScript SDK, Firebase Data Connect web bindings, andFirebase App Hosting. Always pair client-side SDK initialization with Firebase App Check and strict Security Rules, and test locally with the Firebase Local Emulator Suite before deploying.
Where can I watch video tutorials on Firebase Web and AI development?
You can watch our hands-on Firebase video walkthroughs on theCodingCat.dev YouTube Channel, includingFirebase SQL with Data Connect, and browse our complete Firebase tutorial archive on CodingCat.dev.
Top comments (0)