adverse-sapphire
adverse-sapphire13mo ago

Supabase clone with new id

When I am trying to clone a record and hit save I get the error duplicate key value violates unique constraint "contract_pkey" There was an error creating contract (status code: 23505) because it is trying to use the id of the original record as the id of the new record. How can I change this so that it will post the new record without the id so that supabase can dynamically generate an id?
10 Replies
kapa.ai
kapa.ai13mo ago
Hi @ahnaf1022. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: Sorry @ahnaf1022, I seem to be having a bit of trouble. Don't worry though, I've pinged my supprt and they will try to fix me up! :technologist: Perhaps try again in 15mins?
adverse-sapphire
adverse-sapphire13mo ago
@kapa.ai When I am trying to clone a record and hit save I get the error duplicate key value violates unique constraint "contract_pkey" There was an error creating contract (status code: 23505) because it is trying to use the id of the original record as the id of the new record. How can I change this so that it will post the new record without the id so that supabase can dynamically generate an id?
kapa.ai
kapa.ai13mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- In refine, when you clone a record using action: "clone", it fetches the record data according to the id with useOne and returns the queryResult for you to fill the form. After the form is submitted, it creates a new record with useCreate. However, it seems like the id of the original record is being included in the new record, which is causing a conflict as id values need to be unique. To resolve this, you need to ensure that the id of the original record is not included in the new record. This can be done by omitting the id field when you submit the form for the new record. Here's an example of how you might use useForm with action: "clone":
import React from "react";
import { Create, useForm } from "@refinedev/antd";
import { Form, Input, Space, Switch } from "antd";

interface IPost {
id: number;
title: string;
content: string;
}

const PostCreatePage: React.FC = () => {
const { formProps, saveButtonProps } = useForm<IPost>();

return (
<Create saveButtonProps={saveButtonProps}>
<Form {...formProps} layout="vertical">
<Form.Item
label="Title"
name="title"
rules={[
{
required: true,
},
]}
>
<Input />
</Form.Item>
<Form.Item
label="Content"
name="content"
rules={[
{
required: true,
},
]}
>
<Input.TextArea />
</Form.Item>
</Form>
</Create>
);
};
import React from "react";
import { Create, useForm } from "@refinedev/antd";
import { Form, Input, Space, Switch } from "antd";

interface IPost {
id: number;
title: string;
content: string;
}

const PostCreatePage: React.FC = () => {
const { formProps, saveButtonProps } = useForm<IPost>();

return (
<Create saveButtonProps={saveButtonProps}>
<Form {...formProps} layout="vertical">
<Form.Item
label="Title"
name="title"
rules={[
{
required: true,
},
]}
>
<Input />
</Form.Item>
<Form.Item
label="Content"
name="content"
rules={[
{
required: true,
},
]}
>
<Input.TextArea />
</Form.Item>
</Form>
</Create>
);
};
In this example, the id field is not included in the form, so it won't be included in the new record. This should allow Supabase to dynamically generate a new id for the record. For more information, you can refer to the refine documentation (https://refine.dev/docs/api-reference/antd/hooks/form/useForm). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai13mo ago
<@543682490951532554> kapa.ai is still learning and improving, please let us how it did by reacting below
adverse-sapphire
adverse-sapphire13mo ago
here is my form, I haven't included id field in the form but still the error is coming. @kapa.ai
kapa.ai
kapa.ai13mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Hmm, I don't know enough to give you a confident answer yet. It seems like there might be something else in your code that's causing the issue. I would recommend checking the rest of your code to make sure there's nothing else that could be causing the id of the original record to be included in the new record. You can also refer to the refine documentation (https://refine.dev/docs) for more information on how to use refine's features. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai13mo ago
<@543682490951532554> kapa.ai is still learning and improving, please let us how it did by reacting below
adverse-sapphire
adverse-sapphire13mo ago
I haven't included id field in my form but still the error is coming. @kapa.ai
kapa.ai
kapa.ai13mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- I'm sorry to hear that you're still experiencing this issue. Based on the information provided in the extracts, it's not clear why the id field is still being included when you're cloning a record, especially if you're not including it in your form. It's possible that there might be something else in your code that's causing this issue. I would recommend double-checking your code to ensure there's nothing else that could be causing the id of the original record to be included in the new record. If you're still having trouble, it might be helpful to refer back to the refine documentation (https://refine.dev/docs) or seek help from the refine community. They might be able to provide more specific guidance based on your exact code and setup. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai13mo ago
<@543682490951532554> kapa.ai is still learning and improving, please let us how it did by reacting below