Models and Auto Increment ID issue
Support › MB Frontend Submission › Models and Auto Increment ID issueResolved
- This topic has 10 replies, 2 voices, and was last updated 2 months, 1 week ago by
Nicholas Cox.
-
AuthorPosts
-
January 20, 2025 at 7:04 PM #47471
Nicholas Cox
ParticipantHi
When I submit a form on the front end on a new custom table the first entry created takes the next post id and does not start from an ID of 1, why is this?
$meta_boxes[] = [ 'title' => 'Test', 'model' => 'example', 'storage_type'=> 'test' 'table' => 'wp_test', 'id' => 'test', 'fields' => [ [ 'id' => 'product_name', 'type' => 'text', 'name' => 'Product Name', ], ] ]
MB_Custom_Table_API::create( "{$wpdb->prefix}" . 'test', [ 'user_id' => 'BIGINT(20)', 'data' => 'TEXT NOT NULL', 'iv' => 'VARCHAR(50)', ], ['user_id'], true // Must be true for models );
January 20, 2025 at 7:05 PM #47472Nicholas Cox
Participantignore the first code as this was just an example and not the correct code, but the issue still persists with the form code being correct to that of the table structure, i cannot edit my first post.
January 21, 2025 at 1:09 PM #47484Peter
ModeratorHello Nick,
It looks like the ID 1 was deleted in the database. I test to create the first entry of the custom model, the ID is started from 1 as well. But if I delete the entry in the database and submit a new entry, it will start from 2 as it is.
I'm discussing this case with the development team and get back to you later.January 21, 2025 at 3:16 PM #47487Nicholas Cox
ParticipantYes it seems it takes the next available wordpress post ID and not the next available ID of the custom model database table.
January 22, 2025 at 10:18 PM #47501Peter
ModeratorHello Nick,
I don't see it takes the default WordPress post ID instead of the custom model ID. However, if somehow the first custom model entry ID 1 was deleted, the ID will start from 2 and so on. The WordPress post ID also works like that.
You can create a new post, delete it and create another post to see how the ID is generated.January 22, 2025 at 11:19 PM #47510Nicholas Cox
ParticipantHi. Yes try adding a few default wordpress posts first and then try adding a row to the custom models database table. As I'm not sure if your test environment matches to replicate my issue? If not then no worries.
What your describing is normal behaviour in regards to deleting the first databass table entry and adding a new entry which assigns an id of 2 etc... mysql tables behave like this with auto increment columns.
My theory is that metabox is using some logic with posts which is causing the issue. As I have tested manually by inserting a new value and it works as intended with the first row having an id of 1.
Any ideas? Thanks
January 22, 2025 at 11:22 PM #47511Nicholas Cox
ParticipantSo I have over 800 wordpress posts and when I insert the first row into a model table the ID is set to e.g. 806. Its weird behaviour not to be how metabox inserts new row values into custom model tables
January 23, 2025 at 8:45 PM #47524Peter
ModeratorHello Nick,
Can you please share a screen record that demonstrates the entry ID in the model table taking the next ID of WordPress post ID? Because I cannot reproduce that issue on my site.
Thank you.
February 3, 2025 at 5:28 PM #47575Nicholas Cox
ParticipantHi,
I will get a screen recording, but before i do I thought I read somewhere that I can use front end forms with custom models and custom tables now (but cannot find the information), as i posted this a few years ago https://support.metabox.io/topic/front-end-submission-and-custom-models/ which said that this was not supported. Can you confirm if custom models works with front end submission now? as i believe this is the reason why its not working.
thanks
February 3, 2025 at 10:09 PM #47576Peter
ModeratorHello Nick,
The frontend submission form now supports submitting custom models from frontend. When using the frontend form shortcode, you should assign
object_type
tomodel
.
Please read more about this feature in the documentation
https://docs.metabox.io/extensions/mb-frontend-submission/February 5, 2025 at 9:27 PM #47596Nicholas Cox
ParticipantHi Peter,
Ok that makes sense now, I have set the
object_type
tomodel
and tested an all working ok now. Many thanks -
AuthorPosts
- You must be logged in to reply to this topic.