Unable to determine primary key field on custom table - axapta

I have a custom table that I'm basically modeling after the CustGroup table.
The table has two fields, one extends the SysGroup and the other is a Name type. I added an index with AllowDuplicates = No and the one SysGroup field.
And on the table, I set the PrimaryIndex equal to my SysGroup field.
I delete the axapd.aoi file and restarted the AOS. I also ran the cross reference update and SysFlushAOD::main(null);.
When I run the following code, the first line returns 0 and the second 1, meaning it was able to find a primary key.
info(strfmt("MyCustGroup: %1", new SysDictTable(40390).primaryKeyField())); // Returns 0
info(strfmt("CustGroup: %1", new SysDictTable(57).primaryKeyField())); // Returns 1
Any idea what I'm doing wrong?

Your primary key should extend you own extended data type (EDT) extending SysGroup.
In the relations node of the EDT have a normal relation to your table and key field. Then change your key field to extend from your EDT.
Set the TableGroup property of your table to Group.
Then make sure the table succeeds completely the Best Practice check.
If that does not solve your problem, export, delete and import your table.

Related

Views,Entity,Cannot deduce a primary key

I made a view so the back hand of my website is more coherent. Then i tried to map it to my entity schema.
I had the message :
No primary key defined, could not deduce a primary key, table excluded
So i made one ! I made sure it was something unique by combining 3 columns that cannot be identical when combined. And i called that new column "id".
Entity dosen't seems to agree with the unique aspect of my "id" because the error message wont go away and i can't import my view...
Well, i had to use the sql function ISNULL(id,) on my "id" clolumn, making it a not null value so Entity understand that it could NEVER be null and therefore, detecting it as a potential primary key

unable to add a primary key to MS Access table

I am trying to make the "BRFID" field the primary key in this table but I get the following error. (see screenshot). I don't see any other indexed field in the table. I am new to access, what am I doing wrong?
screenshot here
A primary key cannot have any duplicate values. Your problem is that there are duplicates entries in the BRFID field. You need to correct that and make every value in that field unique before it can be a Primary Key field.
In the Query Wizard, there is a way to generate a query that identifies duplicate values. Use that to find the duplicates. You'll have to come up with the way to correct the problem.

Conditionally add columns in SQLite

I've seen enough answers to know you can't easily check for columns in SQLITE before adding. I'm trying to make a lazy person's node in Node-Red where you pass a message to SQLITE which is the query. Adding a table if it does not exist is easy.
msg.topic='create table IF NOT EXISTS fred (id PRIMARY KEY);'; node.send(msg);
it occurred to me that adding a table which had the names of the fields would be easy - and if the field name is not in the table.... then add the field. BUT you can't add multiple fields at once - so I can't do this...
msg.topic='create table IF NOT EXISTS fred (id PRIMARY KEY, myfields TEXT);'; node.send(msg);
The problem with THAT is that I can't add this in later, there's no way to check before adding a field it the table exists!
This is what I WANT
msg.topic='create table IF NOT EXISTS fred (id PRIMARY KEY, myfields TEXT);'; node.send(msg);
msg.topic='if not (select address from myfields) alter table fred add column address text';
I just cannot think of any way to do this - any ideas anyone (the idea is that the node-red node would input a table, field and value and if the table didn't exist it would be created, if the field didn't exist it would be created, all before trying to add in the value).
You won't be able to make the ALTER TABLE conditional in the SQL itself. You'll need to handle that from your calling script.
Alternately, simply attempt to add the column to the table and accept failure as an outcome. Assuming the table exists, the only failure reason you could encounter is that the column already exists.
If you'd like to do something more graceful, you can check if the column exists in advance, then conditionally run the SQL from your calling application.

Wrong autoincrement value in sqlite using JPA

I'm working on a simple project using sqlite, JPA and eclipseLink.
First I create my Person table in my database with this:
CREATE TABLE Person (
idPerson INTEGER PRIMARY KEY AUTOINCREMENT,
firstname TEXT DEFAULT 'NULL',
birthdate DATETIME DEFAULT 'NULL'
)
and then add a new test entry (in order to generate the sqlite_sequence table)
INSERT INTO [Person] ([firstname], [birthdate]) VALUES ('Foo', '1145-11-12 00:00:00')
All the successive insertion are done using JPA, where in the Person class I use this notation for the person id:
#GeneratedValue(generator="sqlite_person")
#TableGenerator(name="sqlite_person", table="sqlite_sequence",
pkColumnName="name", valueColumnName="seq",
pkColumnValue="Person")
#Column(name="idPerson")
private int id;
The first JPA insertion is ok (that is, new new inserted person has id = 2) but then I get an increment of 50 instead of only 1 (so the third inserted person has id = 52, the fourth 102 and so on).
I read that "making modifications to this table will likely perturb the AUTOINCREMENT key generation algorithm" [ref]
Is my problem related to this, even if in theory I'm not modifying that table?
Any kind of suggestion in order to resolve the problem?
You are using Autoincrement in the database, which means the database will assign a value to the id when it is inserted, but then you tell the JPA provider to use Table generation. Table generation requires that the JPA provider use a special table to keep track of sequence values, looking it up and assigning it before inserting the entity row in the database. This conflicts with what you set up in the database.
http://wiki.eclipse.org/EclipseLink/Examples/JPA/PrimaryKey explains sequencing fairly well. You will need to try using #GeneratedValue(strategy=GenerationType.IDENTITY) instead of table generation so that inserts by JPA use the same sequence allocation as inserts outside of JPA.
At the end, in order to solve the problem, I just added two optional elements for the TableGenerator annotation (i.e. initialValue, allocationSize).
So the new annotation for the ID is like this:
#GeneratedValue(generator="sqlite_person")
#TableGenerator(name="sqlite_person", table="sqlite_sequence",
pkColumnName="name", valueColumnName="seq",
pkColumnValue="Person",
initialValue=1, allocationSize=1)
#Column(name="idPerson")
private int id;
I think it works also without the initial value, but like this I also avoid to insert random entries (because it seems that the sqlite_sequence table is automatically generated already when I create the Person table)

asp.net Entity Framework/ Update from database/ The table/view does not have a primary key defined and no valid primary key could be inferred

One of the database view I am trying to import using entity framework contains only two columns, one is an integer type of column and another one is an aggregate function. I am getting the following error.
The table/view does not have a primary key defined and no valid primary key could be inferred. This table/view has been excluded. To use the entity, you will need to review your schema, add the correct keys, and uncomment it.
I understand it is a known scenario and it can be fixed by either including a Key column in the view or modifying the edmx file manually.
I just wanted to know if there is some other solution other than the above two? I do not want to include an additional column in my query and making changes in edmx is not feasible as DB changes are very frequent and the edmx will be overwritten every time I update from db.
You can mark both properties as entity key directly in the designer but you must ensure that the composite value of these two properties will be always unique. If you cannot ensure that you must add another unique column anyway or you may have some other problems when working with such entity set.

Resources