disabling auto-increment in datanucleus? - mysql

I have 2 my-sql databases (live-db, test-db). I need to transfer some tables from live-db to test-db in datanucleus jdo implementation.
The problem is, if primary-key is an auto-increment then it does not transfer same value, but it generates new value.
Is there any method disable the auto-increment in datanucleus as programmatically?

spent 5 min and cannot find the answer, so i think easiest way is to do the following:
1) add new INT column without AUTO_INCREMENT
2) copy column value
3) drop AUTO_INCREMENT column

So you could use one set of metadata for one datastore (with autoincrement) and one set for the other (without). JDO persistence is simply following your instructions in auto-increment

Related

Hibernate: How does native identifier generation strategy work

Hibernate has an identifier generation strategy called native that selects identity, sequence or hilo depending upon the capabilities of the underlying database. I used MySQL with hibernate.hbm2ddl.auto=update which generated id BIGINT(20) NOT NULL AUTO_INCREMENT for id property of Long Java data type.
I am trying to understand how did Hibernate choose AUTO_INCREMENT when it used SchemaExport tool. Is AUTO_INCREMENT the default primary key generation strategy for MySQL?
Could somebody help me understand it?
Hibernate when selecting the key generation mechanism in native mode, will try to choose the best mechanism available in the database. In the case of MySQL, auto increment is available and Hibernate uses it instead of a sequence, because the auto increment mechanism is slightly better altough sequences also work fine.
The reason why it's better is that it's possible in one single JDBC prepared statement, for example an insert, to do an insert AND retrieve the generated key without querying the database - see here for further details.
In the case of sequences, Hibernate has to first call the sequence at some point and then use the value or the result of it's use in a formula to populate the insert key and then issue the insert.
The autoincrement spares this extra roundtrip to the database needed to increment the sequence, and that is the reason why Hibernate prefers it in the case of MySQL.

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).

Postgresql setting next id to write to

I recently migrated a database from mysql to pgsql 9.
But now when I try to create a new object (in django admin) it tells me that the id I'm trying to use (started at one and has increased each time I tried) is already used.
I'm guessing that there is a pointer or index which needs to be set to the last used id. Am I correct?
When you define your table, the PostgreSQL equivalent to 'auto_increment' is:
CREATE TABLE foo (
id SERIAL,
...
);
If your table is already created (as I suspect it is), you can add this manually:
CREATE SEQUENCE foo_id_seq;
ALTER TABLE foo ALTER COLUMN id SET DEFAULT nextval('foo_id_seq');
Note that if you want to stick with the default name that Pg would have given you use the following format for your sequence name:
<table name>_<column name>_seq
Thus in my example, foo_id_seq.
If the table was migrated and it uses serial to replace the mysql auto increment column, your data was probably migrated without incrementing the serial sequence. Look up the postgresql setval function to set your sequence to a value above the highest existing key in your table.

No such table SQLITE_SEQUENCE from AIR/Actionscript

I'm trying to reset the seed for an autoincrement field in SQLITE using AIR/ActionScript/Flex 4.5.
I'm doing what should normally work:
DELETE FROM SQLITE_SEQUENCE WHERE NAME = 'myTable'
I've checked that the database actually contains a SQLITE_SEQUENCE table. I'm able to execute the above statement without errors directly via the Firefox SQLITE plugin (SQLite Manager).
However, when I try to do the same using actionscript, I get an error:
No such table 'SQLITE_SEQUENCE'.
All I could find in searching around was this one guy who posted everywhere he could find - only to be left without an answer:
here
and
here
and
here
Any ideas?
sqlite_sequence table is not created, until you define at least one autoincrement and primary key column in your schema.
You should to insert "Auto increment" to primary key column
at least to one table,
Then SQLite is creating "SQLITE_SEQUENCE" table.
To get all tables have Auto increment:
SELECT * FROM SQLITE_SEQUENCE;

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.