0

I have written some code that will take in user input from a few input boxes, and pass them to the corresponsing table on Supabase.

This is what the code looks like

const initialState = { solution_id: '', organization_id: '', budget_usd: '', other_info: '', country: '', project_duration_days: '', status: ''}
export default function Projects({ Projects }) {
  useEffect(() => {
    console.log(Projects)
  }, [])

  const [newProject, setProject] = useState(initialState)

  console.log("User inputed data")
  console.log(newProject)
  const { solution_id, organization_id, budget_usd, other_info, country, project_duration_days, status } = newProject
  console.log(solution_id)
  console.log(budget_usd)
  console.log(country)

  const router = useRouter()
  function onChange(e) {
    setProject(() => ({ ...newProject, [e.target.name]: e.target.value }))
  }

  async function createNewProject() {
    if (!solution_id || !organization_id || !country) return
    console.log("Got to here")
    console.log(newProject)

    console.log("Got to here now")
    console.log(newProject.solution_id)
    console.log(newProject.country)
    console.log("Got to here now")

    let { data, error } = await supabase
      .from('Projects')
      .insert({ solution_id: newProject.solution_id, organization_id: newProject.organization_id, budget_usd: newProject.budget_usd, other_info: newProject.other_info, country: newProject.country, project_duration_days: newProject.project_duration_days, status: newProject.status })
      .single();

    console.log(data, error)

    router.push(`/projects`)


  }

This is what the supabase table for the Projects table looks like

Image of Projects database

However, everytime I press the submit button, I get this error on my browser

Image of error received

I am not sure why it keeps throwing this error, as I am not inputting any data that is overiding the primary key of the database

Yan Xu
  • 27
  • 1
  • 5

2 Answers2

0

This is complaining because your table's primary key (id) is not being auto-incremented so its trying to save a record with an id that already exists.

Andrew Smith
  • 1,224
  • 6
  • 9
0

uncheck your primary key checkbox and after that click on gear icon and check "is Identity" . then check primary key check box again . This instruction will automatically increase your ID column, so it can never be duplicated.see this screenshot

Mojito
  • 51
  • 3