MySQL backend & Access (.accdb, 2013) Frontend Auto Increment Issue - mysql

I have a MySQL database that the tables are each set up with an ID field which is an auto increment and primary key. When I use access to link to this database and tables in table design view in Access the fields are listed as the primary key but the type is number, not auto number. This is causing an issue when trying to create a new record because the ID field is not being populated automatically. Please help.

The autonumbering property of the id fields is set at the level of the MySQL database. Access might not be able to identify this property when connected to the back end database. This is why it considers it as a number, and not an autonumber, field.
This said, when inserting new records in the MySQL database, you do not need to send any value for the id field, so the MySQL engine automatically fills it with the updated autonumber value.

Related

MS Access: How to automatically populate a PK that is also a FK to another tables auto increment PK?

I'm working on updating a MS Access application that is connected to a MySQL database through an ODBC connector. I'm currently stuck on an issue where I have a form thats record source is a query that joins multiple tables, the 2 tables of issue are roughly structured as shown below.
Table1 {
id int not null auto increment,
...,
primary key(id)
}
Table2 {
table1ID int not null default 0,
...,
primary key(table1ID),
constraint `fk` foreign key (table1ID) references Table1 (id) on delete cascade on update cascade
}
The problem being that Table2's primary key is also a foreign key that references Table1's id and because they are joined in the same query for the record source they both get created at the same time. So when I attempted to add a new record in MS Access, it won't save the record because Table2's "table1ID" is null but Table1's "id" is auto increment so isn't created until the record is first saved. So my question is... is there a way to have MS Access/MySQL automatically fill in Table2's "table1ID" with the auto increment value from Table1's "id" so that MS access is able to save the record?
The error message that gets returned upon trying to save a new record is as follows:
Run-time error '3155':
ODBC--insert on a linked table 'Table2' failed.
[MySQL][ODBC 8.0(a)Driver][mysqld-8.0.27]Column 'table1ID' cannot be null (#1048)
You have to execute a save in the form that creates the table 1 record.
In fact, this is also why Access will execute a automatic record save in a form when you move focus to the sub form. Access does this, since the autonumber ID on a new reocrd is NOT created until you save the reocrd (at least this is the case when using a ODBC data source). With access only, the reocrd save is not required to generate the PK id, but with sql server, or MySql - you MUST save the record.
As noted, with a form + sub form, access does this automatic.
However, say I am on a form, just added a new reocrd, and now i want to run some code, or launch another form? I MUST force a reocrd save first to generate the PK autonumber.
So, code will look like this:
if me.dirty then me.Dirty = false ' save reocrd
' now do your other business here, code that follows, or
' even launch another form, or whatever.
So, a simple force of form record save with me.Dirty = false will force the autonumber generation.
I don't know how MySQL works with a join between two tables, but as a general rule only ONE of the two tables in the join can be updated at a given time.
Thus, you have to break out the steps here, or not allow both tables to be edited (or at least dirty at the same time).
Access does and did allow both tables to become dirty, but server side systems to my knowledge do not. So, you could while editing on the form, when you tab out of a control that is based on the one record, you would execute a me.Dirty = false to generate the auto PK.
However, I don't recommend allowing editing of a form that is based on more then one table at a given time - most server systems don't allow this.
So, add, and save that first record before any child record is attempted to be added here.

How to prevent mySQL autoincrement from resetting in Django

I am creating a Django app where the primary keys are AutoFields. i.e. I am not manually assigning any field as primary key in my models.
I need to use mySQL.
I will need to export all the data to excel or perhaps another django app from time to time. Therefore the primary keys must be unique to be able to identify new records or records to be deleted in excel/other app.
However, I have read that mySQL autoincrement counter resets to the max key when database restarts. This will result in reassignment of keys if the latest records were deleted.
I need to avoid this. No key should be reassigned.
How can this be done?
MySQL 8.0 now keeps the last auto-increment per table persistently. So it remembers between restarts, and does not reset the auto-increment.
https://www.percona.com/blog/2018/10/08/persistence-of-autoinc-fixed-in-mysql-8-0/

Update all rows except identity column in Access 2013

I have a form in Access linking to a SQL Server table. This table has a UID. After I create a new record in Access, it tries to update the table (including the UID) and gives me the error:
Explicit value must be specified for identity column in table 'TableName' either when IDENTITY_INSERT is set to ON or when a replication user is inserting into a NOT FOR REPLICATION identity column.
Now, I understand why this error is occurring but am not sure how to get around it. I can't change IDENTITY_INSERT, and the UID column in Access links the form to a subform, so I do need to include it in the form. How do I exclude only the UID column from the ODBC update? I know I can use a macro to explicitly define columns to update, but there must be an easier way. Is there a function like Me!UIDTextbox.Undo I can run in the Before Update event?
Some more detail:
There's no VBA module or query in Access. It's a standard SQL server table and the Access form updates the table on record change (the default for Access). The table is linked with the standard ODBC Database connection, with the UID as primary key.
This may be a global problem with using Identity columns as Primary Keys in Access.
Patch idea and possible workarounds at
Problem with identity fields in MS-Access

Error with inserting into mysql database

I am using cfwheels (coldfusion orm framework).
I recently moved some data from my previous host to a new one. Now I am trying to insert into a table, but am getting an error message: "Error Executing Database Query.
Duplicate entry '13651' for key 'PRIMARY'"
I looked into the database and it appears a record with id 13651 already exists. So I think the problem is with mysql generating the right auto increment value.
It seems Auto_Increment value is damaged or not set to max value in that column. It's possible due to bulk insert.
So as per solution, set the maximum PK value + 1 as new AUTO_INCREMENT value. Now when you insert the records in this table, they will automatically pick the next incremented correctly.
ALTER.TABLE tablename AUTO_INCREMENT = value
Is the rest of the data for that record, and the one you are trying to insert, the same? If you you might just need to tell the ORM to replace that value?
If primary key has auto increment attribute turned on, do not insert it manually. remove that primary key part from your insert query (whatever the syntax according to the taste of your ORM framework).

Inserting a record with specified primary key into MySQL via Entity Framework

We began using Entity Framework with MySQL in our project recently. Now I am writing unit tests for the data access level; for that purpose I have created a database with some test data.
In the test-method for the Delete method I want to first delete a specified record and then insert it again with all the fields holding exactly the same values, including the Id column which is set as the primary key. The purpose is to keep the test data in the DB.
But when I insert previously deleted record, Entity Framework just ignores the Id value of the entity and thus a new Id is generated using AUTO_INCREMENT.
Thanks in advance.
AUTO_INCREMENT means that you do not get to set the Id value, period. You just can't do this.
What you should do instead is:
Insert a new row.
SaveChanges()
Now read the Id value on the object, which will have been updated from the database.
Go ahead and delete the row, using this Id value.
Have you used the insert ignore statement?
It helps you forcefully insert pks with specified value.