Unable to link a field to table via Xata SDK (JS/TS)

hello, I have been trying to link a role field in one table to a record in the roles table via the Xata SDK, but it does not seem to work. For more context, I am trying to create a new user and the role field is a link to the roles table. In the schema definition, the role cannot be null, but console.loging the return value from db.users.create returns the created user without the link under the role field. I have tried manually passing in the ID to the role I want to link, but that returns Fig. 1 and the new user record is not created If I try to pass the actual value I want from the roles table i.e. roles.role e.g. being == 'user', the create command is successful but users.role is still null. see Fig.2. I have also tried querying the DB for the role I want, and then passing it as value for users.role, if I pass the full object i.e, userRole, or userRole.xata_id I get the error in Fig.1 if I pass the UserRole.role I get the error from Fig.2 I am using a Postgres-enabled workspace and Dedicated clusters
No description
No description
1 Reply
cmck
cmck6d ago
Hi @Kaligraphy , thanks for reaching out. Let me take a look and see if I can shed some light on what's going on. I've not been able to recreate this issue in my own environment. I created two simple tables with a link and I was able to create a record passing the target record id as a string.
import { getXataClient } from "./xata";
const xata = getXataClient();

const record = await xata.db.users.create({
name: "John",
role: "rec_ctg4jttqrj63ucd4j4rg",
});

console.log(record);
import { getXataClient } from "./xata";
const xata = getXataClient();

const record = await xata.db.users.create({
name: "John",
role: "rec_ctg4jttqrj63ucd4j4rg",
});

console.log(record);
which returns...
{"name":"John","role":{"xata_id":"rec_ctg4jttqrj63ucd4j4rg"},"xata_createdat":"2024-12-16T15:49:14.012Z","xata_id":"rec_ctg4nuhhj0j12ftaq750","xata_updatedat":"2024-12-16T15:49:14.012Z","xata_version":0}
{"name":"John","role":{"xata_id":"rec_ctg4jttqrj63ucd4j4rg"},"xata_createdat":"2024-12-16T15:49:14.012Z","xata_id":"rec_ctg4nuhhj0j12ftaq750","xata_updatedat":"2024-12-16T15:49:14.012Z","xata_version":0}
Have you since deleted the users and roles table? If so, please recreate the tables as you did before so I can take a look at them via support access. Also, if you could share the create statements you used, that would be helpful. Feel free to DM me if anything contains sensitive info or contact me via [email protected]
Want results from more Discord servers?
Add your server