Help Needed: Error Creating Profile - "'id' of relation 'Profile' violates not-null constraints

the code : const { formProps, saveButtonProps, form } = useForm( { meta: { select: "*" }, onMutationSuccess: (response) => { console.log('data : ', response.data); handleUserCreationSuccess(response.data); } } ); const { mutate: createProfile } = useCreate(); const { mutate: updateUser } = useUpdate(); const handleUserCreationSuccess = async (userData: BaseRecord) => { const userId = await userData.id; console.log('userId : ', userId); // default profile is created on creation of the User createProfile({ resource: "Profile", values: { username: "", // Default values isEmailVerified: false, emailVerified: null, image: "", twitterUsername: "", twitterFollowers: 0, }, }, { onSuccess: (profileResponse) => { const profileId = profileResponse.data.id; console.log('profileId : ', profileId); // Update the user with the profile ID updateUser({ resource: "User", id: userId, values: { profileId, }, }); }, onError: (error) => { notification.error({ message: "Error", description: `Failed to create profile: ${error.message}`, }); } }); }; Schemas : model User { id Int @id @default(autoincrement()) name String? email String? @unique profileId String? role UserRole? profile Profile? @relation(fields: [profileId], references: [id] } model Profile { id String @id @default(cuid()) username String? isEmailVerified Boolean? emailVerified DateTime? image String? twitterUsername String? twitterFollowers Int? User User[] }
3 Replies
kapa.ai
kapa.ai2mo ago
Hi @Spid€r. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The error message you're seeing is related to the database constraints. It seems like you're trying to create a Profile without providing an id, which is a required field in your Profile model. In your useCreate hook for creating a profile, you're not providing a value for the id field. According to your Profile model, id is a required field and it has a @default(cuid()) directive, which means it should automatically generate a unique id if none is provided. However, it seems like this is not happening. You can try to manually generate a unique id for the Profile when you're creating it. Here's an example of how you can do it:
import { nanoid } from 'nanoid';

// ...

const handleUserCreationSuccess = async (userData: BaseRecord) => {
const userId = await userData.id;
console.log('userId : ', userId);

// default profile is created on creation of the User
createProfile({
resource: "Profile",
values: {
id: nanoid(), // generate a unique id
username: "", // Default values
isEmailVerified: false,
emailVerified: null,
image: "",
twitterUsername: "",
twitterFollowers: 0,
},
}, {
onSuccess: (profileResponse) => {
const profileId = profileResponse.data.id;
console.log('profileId : ', profileId);

// Update the user with the profile ID
updateUser({
resource: "User",
id: userId,
values: {
profileId,
},
});
},
onError: (error) => {
notification.error({
message: "Error",
description: `Failed to create profile: ${error.message}`,
});
}
});
};
import { nanoid } from 'nanoid';

// ...

const handleUserCreationSuccess = async (userData: BaseRecord) => {
const userId = await userData.id;
console.log('userId : ', userId);

// default profile is created on creation of the User
createProfile({
resource: "Profile",
values: {
id: nanoid(), // generate a unique id
username: "", // Default values
isEmailVerified: false,
emailVerified: null,
image: "",
twitterUsername: "",
twitterFollowers: 0,
},
}, {
onSuccess: (profileResponse) => {
const profileId = profileResponse.data.id;
console.log('profileId : ', profileId);

// Update the user with the profile ID
updateUser({
resource: "User",
id: userId,
values: {
profileId,
},
});
},
onError: (error) => {
notification.error({
message: "Error",
description: `Failed to create profile: ${error.message}`,
});
}
});
};
In this example, I'm using the nanoid function from the nanoid library to generate a unique id. You can install it with npm install nanoid. Please note that this is a workaround and it might be better to investigate why the @default(cuid()) directive is not working as expected. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@719500343477534722> kapa.ai is still learning and improving, please let us know how it did by reacting below
foreign-sapphire
foreign-sapphire2mo ago
const { formProps, saveButtonProps, form } = useForm( { meta: { select: "*" }, redirect:false, onMutationSuccess: (response) => { console.log('data : ', response.data); handleUserCreationSuccess(response.data); } } );
const { mutate: createProfile } = useCreate(); const { mutate: updateUser } = useUpdate(); const handleUserCreationSuccess = async (userData: BaseRecord) => { const userId = await userData.id; console.log('userId : ', userId); if (!userId) { return; } // default profile is created on creation of the User createProfile({ resource: "Profile", values: { id: nanoid(), username: "", // Default values isEmailVerified: false, emailVerified: null, image: "", twitterUsername: "", twitterFollowers: 0, }, }, { onSuccess: async (profileResponse) => { console.log('profileResponse : ', profileResponse); const profileId = await profileResponse.data.id; console.log('profileId : ', profileId); // Update the user with the profile ID updateUser({ resource: "User", id: userId, values: { profileId, }, }); }, onError: (error) => { notification.error({ message: "Error", description: Failed to create profile: ${error.message}, }); } }); }; here in the onSuccess section when the profile is created im getting the value undefined in the profileId variable why ? and also the profileResponse is undefined. ???