I’m trying to add a 1-to-1 relation with User <> Profiles (new class) in the database.
I have userId column in Profiles. This is only working when I set this column to a string and not a Relation.
Is this correct that a column to 1-to-1 relation should just be a string?
If not then how should I be setting the relation below?
const saveProfile: SubmitHandler<IFormInput> = async (data) => {
if (!data) return;
try {
const currentUser = await Moralis.User.current();
const User = Moralis.Object.extend('_User');
const query = new Moralis.Query(User);
const userDetails = await query.first();
const Profiles = Moralis.Object.extend('Profiles');
const getProfile = new Moralis.Query(Profiles);
const profile = await getProfile.equalTo('userId', currentUser?.id).first();
if (profile === undefined) {
const newProfile = new Profiles();
if (data.email) {
userDetails?.set('email', data.email);
}
if (data.stableName) {
newProfile.set('stableName', data.stableName);
}
if (data.userWebsite) {
newProfile.set('userWebsite', data.userWebsite);
}
if (data.userInstagram) {
newProfile.set('userInstagram', data.userInstagram);
}
if (data.userTwitter) {
newProfile.set('userTwitter', data.userTwitter);
}
// Set 1-to-1 relation - sets current user ID as userID column in Profiles
newProfile.set('userId', currentUser?.id);
await newProfile.save();
await userDetails?.save();
refetchUserData();
toastSuccess();
}
} catch (err) {
console.error(err);
}
}