I'm trying to insert values into my XAMPP MySQL server using AdonisJs, but getting the following error:
insert into posts
(body
, created_at
, title
, updated_at
) values (DEFAULT, '2018-06-28 15:06:02', 'post 4', 'this is post 4', '2018-06-28 15:06:02') - ER_WRONG_VALUE_COUNT_ON_ROW: Column count doesn't match value count at row 1
What this error tells me is that I am providing 5 values, for a table that consists of 4 values. In fact the table 'posts' does consist of 5 values which are:
id, title, body, created_at, updated_at
I can manually input values into id, title, body, using phpMyAdmin, the table will be updated, and my application will reflect the change.
The only issue I can track this to, seems to be with the increments() method in my PostController.js file. Is it not doing its job correctly?
How do I proceed to figure out whats going on here?
Migration file posts_schema.js :
'use strict'
const Schema = use('Schema')
class PostsSchema extends Schema {
up () {
this.create('posts', (table) => {
table.increments()
table.string('title')
table.string('body')
table.timestamps()
})
}
down () {
this.drop('posts')
}
}
module.exports = PostsSchema
controller PostsController.js:
'use strict'
// Bring in model
const Post = use('App/Models/Post')
class PostController {
async index({ view }){
// const posts = [
// {title:'Post One', body:'This is post one'},
// {title:'Post Two', body:'This is post one'},
// {title:'Post Three', body:'This is post one'}
// ]
const posts = await Post.all();
return view.render('posts.index', {
title: 'Latest Posts',
posts: posts.toJSON()
})
}
async details({ params, view}) {
const post = await Post.find(params.id)
return view.render('posts.details', {
post: post
})
}
async add({ view }) {
return view.render('posts.add')
}
async store({ request, response, session }) {
const post = new Post();
post.title = request.input('title')
post.body = request.input('body')
await post.save()
session.flash({ notification: 'Posted Success'})
return response.redirect('/posts')
}
}
module.exports = PostController
The view add.edge:
@layout('main')
@section('content')
<a href="/posts">Go Back</a>
<hr>
<h1>Add Post</h1>
<form action="/posts" method="POST">
{{ csrfField() }}
<div class="form-group">
<label>Title</label>
<input type="text" name="title" class="form-control" placeholder="Title">
</div>
<div class="form-group">
<label>Body</label>
<textarea name="title" class="form-control" placeholder="Body"></textarea>
</div>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
@endsection
The routes file routes.js :
'use strict'
const Route = use('Route')
Route.on('/').render('home')
Route.get('/posts', 'PostController.index')
Route.get('/posts/add', 'PostController.add')
Route.get('/posts/:id', 'PostController.details')
Route.post('/posts', 'PostController.store')
I have done very little to configure XAMPP. My changes to config.inc.php were simply:
$cfg['Servers'][$i]['controluser'] = 'root';
$cfg['Servers'][$i]['controlpass'] = '123456';
$cfg['Servers'][$i]['auth_type'] = 'config';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = '12345';
/* Server parameters */
$cfg['Servers'][$i]['host'] = '127.0.0.1';
$cfg['Servers'][$i]['compress'] = false;
$cfg['Servers'][$i]['AllowNoPassword'] = true;
been working through this code and tutorial from: Getting Started with AdonisJs https://www.youtube.com/watch?v=SaXhwbuMTu4