docs/docs/schema/ms-sql-server/using-existing-database.mdx
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
When you have an existing database with a schema already present, you don't need to create tables or views or run DDL queries through the Hasura Console.
All you need to do is indicate to Hasura GraphQL Engine which tables and views you want to expose over GraphQL and how they are connected to each other so that you can query them as a "graph".
Tracking a table or a view means telling Hasura GraphQL Engine that you want to expose that table/view over GraphQL.
Data -> Schema section of the Console.Untracked Tables/Views, click on the Track button next to the table/view name.To track the table and expose it over the GraphQL API, add it to the tables.yaml file in the metadata directory as
follows:
- table:
schema: dbo
name: <table name>
Apply the Metadata by running:
hasura metadata apply
To track a table and expose it over the GraphQL API, use the mssql_track_table Metadata API:
POST /v1/metadata HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
"type": "mssql_track_table",
"args": {
"source": "<db_name>",
"schema": "dbo",
"name": "<table name>"
}
}
Data -> Schema section of the Console.Untracked Tables/Views, click the Track All button.To track all tables and expose them over the GraphQL API, add them to the tables.yaml file in the metadata directory
as follows:
- table:
schema: dbo
name: <table-name-1>
- table:
schema: dbo
name: <table-name-2>
To automate this, you could add the tables in a loop through a script.
Apply the Metadata by running:
hasura metadata apply
To track all tables and expose them over the GraphQL API, use the mssql_track_table Metadata API:
POST /v1/metadata HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
"type": "bulk",
"args": [
{
"type": "mssql_track_table",
"args": {
"source": "<db_name>",
"schema": "dbo",
"name": "<table-name-1>"
}
},
{
"type": "mssql_track_table",
"args": {
"source": "<db_name>",
"schema": "dbo",
"name": "<table-name-2>"
}
}
]
}
To automate this, you could add the mssql_track_table requests to the bulk request in a loop through a script.
Tracking a foreign-key means creating a relationship between the tables involved in the foreign-key.
Data -> Schema section of the Console.Relationships tab.Add, give a name to your relationship
(this will be the name of the nested object in the GraphQL
query), and hit Save to create the relationship.To track a relationship and expose it over the GraphQL API, add it to the tables.yaml file in the metadata directory
as follows:
Object relationship
- table:
schema: dbo
name: <table name>
object_relationships:
- name: <relationship name>
using:
foreign_key_constraint_on: <reference column>
Array relationship
- table:
schema: dbo
name: <table name>
array_relationships:
- name: <relationship name>
using:
foreign_key_constraint_on:
column: <reference column>
table:
schema: dbo
name: <reference table name>
Apply the Metadata by running:
hasura metadata apply
Object relationship
You can create an object relationship by using the mssql_create_object_relationship Metadata API:
POST /v1/metadata HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
"type": "mssql_create_object_relationship",
"args": {
"source": "<db_name>",
"table": "<table name>",
"name": "<relationship name>",
"using": {
"foreign_key_constraint_on": "<reference column>"
}
}
}
Array relationship
You can create an array relationship by using the mssql_create_array_relationship metadata API:
POST /v1/metadata HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
"type": "mssql_create_array_relationship",
"args": {
"source": "<db_name>",
"table": "<table name>",
"name": "<relationship name>",
"using": {
"foreign_key_constraint_on" : {
"table" : "<reference table name>",
"column" : "<reference column>"
}
}
}
}
Data -> Schema section of the Console.Untracked foreign-key relations, click the Track All button to automatically create
relationships based on the foreign-keys.To track all relationships and expose them over the GraphQL API, add them to the tables.yaml file in the metadata
directory as follows:
Object relationship
- table:
schema: dbo
name: <table name>
object_relationships:
- name: <relationship name>
using:
foreign_key_constraint_on: <reference column>
Array relationship
- table:
schema: dbo
name: <table name>
array_relationships:
- name: <relationship name>
using:
foreign_key_constraint_on:
column: <reference column>
table:
schema: dbo
name: <reference table name>
To automate this, you could add the relationships in a loop through a script.
Apply the Metadata by running:
hasura metadata apply
You can create multiple relationships by using the mssql_create_object_relationship and the mssql_create_array_relationship Metadata APIs:
POST /v1/metadata HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
"type": "bulk",
"args": [
{
"type": "mssql_create_object_relationship",
"args": {
"source": "<db_name>",
"table": "<table name>",
"name": "<relationship name>",
"using": {
"foreign_key_constraint_on": "<reference column>"
}
}
},
{
"type": "mssql_create_array_relationship",
"args": {
"source": "<db_name>",
"table": "<table name>",
"name": "<relationship name>",
"using": {
"foreign_key_constraint_on" : {
"table" : "<reference table name>",
"column" : "<reference column>"
}
}
}
}
]
}
To automate this, you could add the create relationships requests to the bulk request in a loop through a script.
:::info Relationship nomenclature
In this case, Hasura GraphQL Engine will automatically generate relationship names (the names of the nested objects in the GraphQL query) based on the table names and the foreign-key names.
The name is generated in the following format:
singular of foreignTableNameplural of foreignTableNameFor example, for the foreign-key article.author_id -> author.id, the relationship names will be author for article
table and articles for author table.
In case a field with the generated name already exists, a new name will be generated of the form:
camel case of (singular/plural of foreignTableName + _by_ + foreignKeyColumnName)
Note that, this is just an arbitrary naming convention chosen by Hasura to ensure the generation of unique relationship names. You can choose to rename your relationships to anything you wish. You can change the relationship names with a name of your choice as shown in MS SQL Server: Renaming relationships.
:::