Migrating an AnyDAC app to FireDAC fails on the AutoInc fields - postgresql

I have migrated an AnyDAC app to FireDAC and I can't get to work its Autoinc fields.
The ID field (primary key) has been defined on Postgre SQL as default to nextval('llistapanelspuzzle_id_seq'::regclass), BIGSERIAL, so the server automatically sets its values.
The column was recognized by AnyDAC as an TAutoincField and worked correctly, but when I now open that table on FireDAC it fails saying that the field found is a TLargeIntField. I change the persistent field to a TLargeIntField, but now when inserting records on Delphi, I don't get the new values from the server, it leaves the dataset with a 0 value, and when I add a second record it raises a Key Violation (two records with a 0 value on its primary key).
Do you know how to define AutoInc fields on FireDAC - PostgreSQL, when they are being recognized as LargeInt fields ?.
Update: I have added ID to the UpdateOptions.AutoIncFields, but it doesn't seem to have changed anything.
Thank you.

Looks like you have to activate the ExtendedMetada flag on the FDConnection in order for FireDAC to recognize automatically the PostgreSQL Autoinc columns.
Now it works correctly.

Related

Is it possible to get another field of row I'm trying to duplicate in PSQL or MyBatis?

I have a table 'client', which has 3 columns - id, siebel_id, phone_number.
PhoneNumber has a unique constraint. If I save a new client with an existing number, I'll get an error ERROR: duplicate key value violates unique constraint "phone_number_unique".
Is it possible to make PSQL or MyBatis showing 'siebel_id' of a record where the phone number already saved?
I mean to get a message like
'ERROR: duplicate key value violates unique constraint "phone_number_unique"
Detail: Key (phone_number)=(+79991234567) already exists on siebel_id...'
No, it's not possible to tweak the internal message that the PostgreSQL database engine returns accompannying an error. Well... unless you recompiled the whole PostgreSQL database from scratch, and I would assume this is off the table.
However, you can easily search for the offending row using SQL, as in:
select siebel_id from client where phone_number = '+79991234567';

MS Access - PostgreSQL auto number

I have a frontend in ms access and a backend in Postgresql. The database used to be on SQL Server before moving onto Postgresql. Most tables have a column called ID that is an AutoNumber in Access, so it gets the next ID automatically. It used to work fine on simple Access (database started on accdb file) and then when it was moved to SQL server.
After moving to PostgreSQL though the column is now labeled as "Number" in Access and it doesn't give new IDs when a new record is added, this causes a "Primary Key cannot be null" error. The Column ID is set to INTEGER with IDENTITY: Always and an increment of 1. Obviously I cannot alter the table in Access and change number into autonumber because it's read-only as a linked table.
How can I make Ms Access recognize the IDENTIFY column of PostgreSQL as an AutoNumber?
Edit with some more information:
I have a table called Orders and a table called Jobs. Each Order has multiple Jobs and they are connected with an OrderID column. Adding an Order without any Jobs works fine (even though the column is "Number" instead of "AutoNumber" Postgres creates new unique values correctly) The problem comes when I add a Job to that Order, I get "Primary Key cannot be null" error.
I added the Job ID field to be visible on the form and it doesn't have any value while I edit the other columns of the Job form. I used to create a new ID using Me.Dirty = False on the Job adding form, and that's the exact point where the error is reported. So Me.Dirty = False doesn't create a new ID on Postgres, but it creates one just fine on Access or SQL Server.

Instance has a NULL identity key error during insert with session.add() in sqlalchemy into partitioned table with trigger

I am using postgresql and sqlalchemy for my flask project.
I recently partitioned one of my big tables based on created_on using postgresql triggers.
But now if a try to insert a record into master table with db.session.add(obj) in sqlalchemy, i am getting error saying
Instance has a NULL identity key. If this is an auto-generated value, check that the database table allows generation of new primary key values, and that the mapped Column object is configured to expect these generated values. Ensure also that this flush() is not occurring at an inappropriate time, such as within a load() event.
Here I am using a sequence to increment my primary key. Please help me with this.
use autoincrement=True while defining your column example in my code sno is an autoincrement field :
class Contact(db.Model):
sno = db.Column(db.Integer, primary_key=True,autoincrement=True)

Imported data, duplicate key value violates unique constraint

I am migrating data from MSSQL.
I created the database in PostgreSQL via npgsql generated migration. I moved the data across and now when the code tries to insert a value I am getting
'duplicate key value violates unique constraint'
The npgsql tries to insert a column with Id 1..how ever the table already has Id over a thousand.
Npgsql.EntityFrameworkCore.PostgreSQL is 2.2.3 (latest)
In my context builder, I have
modelBuilder.ForNpgsqlUseIdentityColumns();
In which direction should I dig to resolve such an issue?
The code runs fine if the database is empty and doesn't have any imported data
Thank you
The values inserted during the migration contained the primary key value, so the sequence behind the column wasn't incremented and is kept at 1. A normal insert - without specifying the PK value - calls the sequence, get the 1, which already exists in the table.
To fix it, you can bump the sequence to the current max value.
SELECT setval(
pg_get_serial_sequence('myschema.mytable','mycolumn'),
max(mycolumn))
FROM myschema.mytable;
If you already know the sequence name, you can shorten it to
SELECT setval('my_sequence_name', max(mycolumn))
FROM myschema.mytable;

Value for PRIMARY KEY recognized as NULL (EF6 Code First)

Well, I have rather odd exception when I try to insert new value. Please, see attached files for further investigation.
So, I use:
SQL Server 2012
VS 2012 Update 2
EF6 Alpha 3 Code First
The problem is I'm trying to insert new value into simple table called dbo.Worker2 (see "Worker2 Table" image file) which consists of 2 columns: TableNumber and Name. The TableNumber column is primary key of integer data type (see "Primary Key column properties" image file). I have installed the latest EF6 alpha3 and trying to insert new value (see "VB.NET Code" image file). However, exception is thrown: "Cannot insert NULL the value NULL into column 'TableNumber', table Bonus.dbo.Worker2'; column does not allow nulls. INSERT fails." (see "VS Exception" image file).
Then I have decided to see what T-SQL query EF sends to SQL Server with SQL Server Profiler and was surprised that no TableNumber was sent by EF (see "Profiler Queries" image file).
Is it bug or I have missed something?
By the looks of it you need to set your Worker2 Primary Key property as non-database generated.
something like:
<Key, DatabaseGenerated(DatabaseGeneratedOption.None)>
Public Property TableNumber As Integer
I think Primary Key is not set to identity.