Only foreign key constraints are allowed on this table AX 2012 - foreign-key-relationship

I have created three fields in a table: Country, State and City.
I have given EDT as LogisticsAddressCountryRegionId for Country, LogisticsAddressStateId for State. The relations are
MyTable.Country = LogisticsAddressCountry.CountryRegionId.
I am getting a best practise error as Only foreign key constraints are allowed on this table.
I tried few options like Setting New roles in relations. Doesnt work.

The error message is probably misleading. It will be linked to the extended data type with table relation.
Try following
1) Check if the EDT you are using is in the 2012 style or 2009 style (2009 has relations). If it is in the old style try to use new style datatype with table reference instead of relation.
2) Add relation to the table level.
3) "Table1 -> Field1,Field12
Index1 -> Field1 having AlternateKey set to "Yes".
Table1 -> properties->PrimaryIndex set to "Index1".
Table2 -> create a foreign key relation (Foreign key -> Primary key based)
then automatically a reation Table2.Table1 ==Table1.Field1 is created.
In this way you can easily create a relation on any field other than RecId. and also the BP error Only foreign key constraints are allowed on this table will remove."
http://dynamicsuser.net/forums/p/54753/288954.aspx
4) Try to export table into .XPO. Then change EnforceFKRelation property in file and import .XPO back to axapta.
https://erpcoder.wordpress.com/2014/08/04/get-rid-of-bp839-only-foreign-key-constraints-are-allowed-on-this-table/
5) You can find more about this error here http://microsoft-dynamics-ax-erp.blogspot.cz/2012/12/debug-bp-errors-in-dynamics-ax-2012.html
"For those of you who were wondering the reason for this BP error, it is because we should create a new Foreign key based relation instead of a normal relation.
Simply put, when you drag and drop the ItemID EDT on the table, it will ask for your confirmation to add the relation on the EDT.
Press Yes and a Foreign key based relation will be created. If you press No and want to create the relation manually, make sure you create a Foreign key based relation and not a normal relation.
Both the normal and foreign key relation looks the same and it is visually difficult to differnetiate between them. So, if you ever encounter the above mentioned BP and have a relation defined, delete and recreate a new foreign key relation."

You have to delete existing relation and add new relation to the same table. Then add relation fields and select New -> ForeignKey -> PrimaryKey based. AX will create all three fields.

Related

Foreign key constraint is incorrectly formed laravel 7

parcel table
$table->unsignedBigInteger('shop_id');
$table->foreign('shop_id')->references('id')->on('shops');
** I have a model name Shop I want to add its is to id as a foreign key to parcel table **
I think that the problem lies in the order you created your migrations. Look at the order and make sure that shops table comes before the parcels table. If this is not the case, then the easiest way would be to change the table times e.g. date_time_name. Change time and you'll be set.

How to implement a Physical Data Model without Foreign Keys in PowerDesigner

I have a PDM in Power Designer that I need to implement on my Oracle database.
I want to test two cases, one with the constraints like Primary Key & Foreign Key and one without those constraints.
I don't know how to do the 2nd case, do I need to use Unique Indexes ? How can it work like a fk in my diagram ? How can avoid to implement the foreign keys but still have them in my diagram ? Is there an option to not enforce the foreign key in my database ?
I can't find any or proper way to deal with the 2nd case
Thanks for your help
#pascal's comment do the trick :
On in Database > Generate Database, Options tab, in All Objects >
Table & Column > Foreign key, you have a Create foreign key checkbox

Oracle SQL Data Modeler missing a PRIMARY KEY on DDL script export

The diagram has over 40 tables, most of them have a primary key defined.
For some reason there is this one table, which has a primary key defined, but that's being ignored when I export the model to a DDL script.
This is the "offending" key (even though it's checked it is nowhere to be found on the generated DDL script):
Has anybody had the same problem? Any ideas on how to solve it?
[EDIT] This is where the key is defined:
And this is the DDL preview (yes, the primary key shows up there):
This is what happens if I try to generate the DDL for just that table (primary key still not generated):
I was finally able to identify and reproduce the problem.
It was a simple conflict of constraints.
Table MIEMBROS had a mandatory 1 to n relationship (foreign key) from another table on its primary key column and vice-versa (there was a foreign key on MIEMBROS against the other table's primary key).
This kind of relationship between two tables makes it impossible to add a record to any of them: The insert operation will return an error complaining about the foreign key restriction pointing the other table.
Anyway I realized that one of the relationships was 0 to n so I simply unchecked the "mandatory" checkbox on the foreign key definition and everything went fine.
So, in a nutshell: The Data Modeler "fails" silently if you are defining a mutual relationship (two foreign keys, one on each table against the other table) on non nullable unique columns, by not generating the primary key of one of the tables.
Such an odd behavior, if you ask me!
"This kind of relationship between two tables makes it impossible to add a record to any of them: The insert operation will return an error complaining about the foreign key restriction pointing the other table."
Actually, if you have deferred constraints, this is not impossible. The constraints can be enforced, for example, at commit time rather than immediately at insert time.
From the Data Modeler menu under File, I used Export -> DDL File. The keys appeared in the DDL, then when I went back to the diagram and did DDL Preview, it showed all the missing stuff.

How disable checking data integration in create statement (reference to non-existing table)

Let assume that I have an empty database and I want to create two tables. There is a relationship between them. For example, one of attributes (call them b) in R is a foreign key. The definition SHOULD (database is empty, I have not executed any statement, yet) look:
create table R(
a type primary key,
b type references S(b)
);
create table S(
b type primary key
);
If I try run script with statements as above, I get error (according to line: b type references S(b)) , because S doesn't exist - that is normal and natural. It is possible to disable checking existing of tables, coresponding to foreign keys?
I know I can change order of statements. Or create tables without constraints, and add them later.
Why I'm asking. Let assume that we have many tables, with many relationships between them. Ordering tables manually will consume a lot of time, when we will want prepare kind of 'backup script'.
I know that it is possible to disable constraint as below:
alter table table_name disable constraint_name;
But that works for example with insert statement. There is a solution proper to create statement?

Linq2Sql: Can I create entities with foreign key relationships without a primary key in both tables?

I have 2 tables in my database that I'm trying to create Linq2Sql entities for. There's more to them than this, but this is essentially what they come down to:
Rooms UserActivity
-------- --------
RoomID ActivityID
RoomID (foreign key on Rooms.RoomID)
The UserActivity table is essentially just a log for actions a user performs against the Rooms table.
Since the UserActivity table is only used for logging actions taken, it didn't make a lot of sense (to me at least) to create a primary key for the table originally, until the Linq2Sql mapper refused to make UserActivity a part of the Room entity in my Linq entities. When I set up the entities in the Visual Studio designer, I got these 2 warnings:
Warning 1 DBML1062: The Type attribute 'UserActivity' of the Association element 'Room_UserActivity' of the Type element 'Room' does not have a primary key. No code will be generated for the association.
Warning 2 DBML1011: The Type element 'UserActivity' contains the Association element 'Room_UserActivity' but does not have a primary key. No code will be generated for the association.
These warnings led me to create the ActivityID column in my table as displayed above.
What I'd like to know is if there is any way to allow Linq2Sql to create relationships between my entities without having a primary key in both tables. If I don't have the primary key in the UserActivity table, the entities can still be created, but the relationships aren't generated.
Is is it possible to do this, or should I try to make sure my tables always have a primary key in them as a general good practice?
Any table that stores real data in your app should always have a primary key - most cases, in SQL Server environments, a INT IDENTITY(1,1) will be just fine. You don't have to keep track of those, no bookkeeping necessary etc. It doesn't cost you much, totally easy to do - I don't see any reason why not have a primary key, even on your UserActivity table.
ALTER TABLE UserActivity
ADD UserActivityID INT IDENTITY(1,1)
CONSTRAINT PK_UserActivity PRIMARY KEY
and you're done!
The only time I would say no primary key is needed is for things like temporary tables when bulk importing huge amounts of data, or other temporary scenarios.
Marc
You need a primary key to create relationships.
It's good practise to always design tables with primary keys, even if you add surrogate (auto increment identity).

Resources