X
Xata9mo ago
teri

Link to table

I need help in understanding link to table when creating a new schema for a table connecting to another table in the database. Kindly share resources or jump on a call with me to get a clearer picture.
6 Replies
kostas
kostas9mo ago
Hello, links are xata's way of establishing one-to-one relationships between records. In case you are familiar with the term, the xata id of the linked record is used as a foreign key to establish a connection between the two records through the link field. It effectively provides the same result and capabilities as an SQL JOIN on the link column. You can find more information on the docs page here: https://xata.io/docs/concepts/data-model#links-and-relationships Many-to-many relationships can be established with a junction table, meaning that you'd create an extra dedicated table with two link columns, each record in that table representing a relationship between the two links. More details here: https://xata.io/docs/concepts/data-model#many-to-many-relationships-with-links
teri
teriOP8mo ago
Checking it now. Thanks, Kostas. Thanks, @kostas for the resource. I got it, and it worked.
teri
teriOP8mo ago
how can I get the ID from my linked trips table to be referenced in this code? @kostas @tsg
import { XataFile } from "@xata.io/client"; // Generated with CLI
import { getXataClient } from "./xata";
const xata = getXataClient();

const record = await xata.db.notes.create({
name: "string",
description: "longer text",
rating: 3,
trip: "rec_xyz",
type: "string",
img: XataFile.fromBase64("SGVsbG8gV29ybGQ="),
});
console.log(record);
import { XataFile } from "@xata.io/client"; // Generated with CLI
import { getXataClient } from "./xata";
const xata = getXataClient();

const record = await xata.db.notes.create({
name: "string",
description: "longer text",
rating: 3,
trip: "rec_xyz",
type: "string",
img: XataFile.fromBase64("SGVsbG8gV29ybGQ="),
});
console.log(record);
https://github.com/Terieyenike/track-trip-dashboard-with-xata-next/blob/main/utils/notes-data.js https://github.com/Terieyenike/track-trip-dashboard-with-xata-next/blob/main/app/(custom)/dashboard/note/create/page.js https://track-trip-dashboard-with-xata-next.vercel.app/dashboard/note/create
GitHub
track-trip-dashboard-with-xata-next/utils/notes-data.js at main · T...
Keep track of all your adventures, never forget the amazing memories. - Terieyenike/track-trip-dashboard-with-xata-next
GitHub
track-trip-dashboard-with-xata-next/app/(custom)/dashboard/note/cre...
Keep track of all your adventures, never forget the amazing memories. - Terieyenike/track-trip-dashboard-with-xata-next
Track your travel
Keep track of all your adventures, never forget the amazing memories
tsg
tsg8mo ago
when you create the other record you get the ID back. Or you can query it and then you have it in the 'id' column. so for example, if you do:
const record = await xata.db.Users.create({
name: 'Keanu Reeves'
});
const record = await xata.db.Users.create({
name: 'Keanu Reeves'
});
you get back:
{
"email": "[email protected]",
"id": "rec_cd8rqcoavc42pi67lgd0",
"name": "Keanu Reeves",
"bio": NULL,
"address": NULL,
}
{
"email": "[email protected]",
"id": "rec_cd8rqcoavc42pi67lgd0",
"name": "Keanu Reeves",
"bio": NULL,
"address": NULL,
}
and the id is there. Does that make sense?
teri
teriOP8mo ago
I'll give it a shot and give you feedback. Thanks The excitement of fulfillment that I have gotten everything I have asked on this platform. It was a difficult one, but I overcame all the issues that came my way. The last thing to do is to put the finishing touch to the app and add one more feature to complete it. Thanks, @tsg and @kostas for guiding and giving me pointers.
tsg
tsg8mo ago
thank you for the kind words, and I can't wait to see your app launched!
Want results from more Discord servers?
Add your server