Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

mysql - Duplicate entry '1' for key 'PRIMARY' how to fix?

I do it with knex and the fronted is done with vue js. Everthing works but the post not and when i put something in the form it comes Duplicate entry '1' for key 'PRIMARY' how can i fix that?

I do it with knex and the fronted is done with vue js. Everthing works but the post not and when i put something in the form it comes Duplicate entry '1' for key 'PRIMARY' how can i fix that?


exports.up = function(knex) {
  return knex.schema.createTable('series', function(table) {
    table.increments('id').unsigned().primary()
    table.string('text').notNullable()
    table.enum('rating', ['good', 'not bad', 'bad']).defaultTo('not bad')
    table.string('comment').notNullable()
  })
};

exports.down = function(knex) {
  return knex.schema.dropTableIfExists('series')
};

The frontend

template>
  <div class="Series">
    <h2>Table</h2> 
        <table id="tabelle" border="1">
            <!-- Header -->
            <tr>
                <th>text</th>
                <th>rating</th>
                <th>comment</th>
                <th>action</th>
            </tr>
            <!-- series List -->
            <tr v-for="series in series" :key="series.id">
                <td> {{series.text}}</td>
                <td>{{series.rating}}</td>
                <td>{{series.comment}}</td>
                <td><button @click="removeData(series.id)">L?schen</button></td>
            </tr>
        </table>

        <input type="text" name="text" v-model="series.text"> <br>
          <select name="rating" v-model="series.rating">
                <option>good</option>
                <option>not bad</option>
                <option>bad</option>
          </select><br>
          <input type="text" name="comment" v-model="series.comment"> <br>
          <button @click="postData" >Post</button>

  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'Series',
  data() {
    return {
      series: {},
    }
  },
  methods: {
    getData(){
      axios.get('/api/series').then((response) =>{
        console.log(response.data);
        this.series = response.data;
       })
      .catch((error) => {
      console.log(error);
       });
    },
    postData(){
      axios.post('/api/serie/create', this.series)
      .then((result) => {
          console.warn(result)
      }).catch((error) => {
        console.log(error);
      });
    },
    removeData(id){
      axios.delete('/api/serie/delete/'+id).then(()=>{
        this.getData();
      })
    }
  },
  mounted() {
      this.getData()
  },

  
}
</script>

endpoints

app.post('/api/serie/create', (req,res)=> {
  console.log("create");
  console.log(req.body);
  let serie = req.body
  knex('series').insert(serie)
    .then(results => res.json(results))
    .catch(err => {
      console.error(err.sqlMessage)
      res.status(500)
      res.json(err.sqlMessage)
    }) 
})

enter image description here

question from:https://stackoverflow.com/questions/65935473/duplicate-entry-1-for-key-primary-how-to-fix

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This error tells you like what each time you want to save in your database, the primary key is duplicated because your "primary key column" doesn't increment. Check that your "primary key column " is auto-incrementing. If not, or modify to auto-increment or increment it manually.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...