I use doctrine.
It's possible to generate entities who based on a database with table (important) without primary key ?
The DB is for Chacal XXI (an application) and 1 or 2 tables have not primary keys, i can't add primary key manually.
No. From https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/basic-mapping.html#identifiers-primary-keys :
Every entity class must have an identifier/primary key. You can select the field that serves as the identifier with the #Id annotation.
Related
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
There is a name field in the UI which is also the primary key column in the underlying table. There is a requirement to make that field editable in the UI. There should be an ID which should serve as the primary key, but there isn't and now it is not feasible to introduce any ID column.
Is there any alternate design idea which can be used in such a scenario ?
The UI is in Swing and DB is Oracle.
First of all, I don't know, who thinks Name field can be Primary Key. That's the wrong database design ever.
Yes, you better change it to some ID column as Primary Key and that shouldn't be updated in future. Since, you can't have multiple Primary Key. So, you need to perform some circus here.
You need to drop existing Primary Key first. Since, you can't have multiple Primary Key in single table.
Create your ID column and allow NULL
Then, update this column with sequence.
Once your ID column gets populated, you need to create Primary Key on this column.
You can only have one primary key, but you can have any number of unique indexes on a table. So let the existing primary key be the immutable primary key and have the application use this key internally for everything. Add another column to the table and create a unique index on it. Let the users modify this other field.
Another alternative would be to declare all child tables with foreign keys ON UPDATE CASCADE. That way, any update to the primary key will cascade to the child tables. Once implemented in production, quit the company and run fast in the other direction and write an article about how you were the first person ever to use ON UPDATE CASCADE in a production setting.
I want to add to the UserProfile table of the StarterSite database created by the WebMatrix Starter Site template another column named email_pk (varchar 50) as primary key and add to id a foreign key constraint that references the Email column (varchar 50) of a new tb_contacts table. The latter column isn't a primary key.
When I try to do that manually in the WebMatrix Database workspace I get the following error:
There are no primary or candidate keys in the referenced table 'dbo.UserProfile' that match the referencing column list in the foreign key 'FK_tb_admin_user_UserProfile'.
Could not create constraint. See previous errors.
System.Data.SqlClient.SqlException (0x80131904): There are no primary or candidate keys in the referenced table 'dbo.UserProfile' that match the referencing column list in the foreign key 'FK_tb_admin_user_UserProfile'.
Could not create constraint. See previous errors.
at Microsoft.WebMatrix.DatabaseManager.SqlDatabase.SqlDatabaseProvider.EditTable(String connectionString, String schema, TableInfo tableInfo)
at Microsoft.WebMatrix.DatabaseManager.IisDbManagerModuleService.EditTable(DatabaseConnection databaseConnection, String schema, Object tableInfoData, String configPathState)
at Microsoft.WebMatrix.DatabaseManager.Client.ClientConnection.EditTable(String schema, Object tableInfoData)
at Microsoft.WebMatrix.DatabaseManager.Client.ClientTable.CommitChanges()
at Microsoft.WebMatrix.DatabaseManager.Client.TableDesignerViewModel.PerformSave()
ClientConnectionId:1da00f40-8f46-4c5b-b423-905c6990fd0d
If you want to add a foreign key to your UserProfile table, the referenced column must be a primary key or have a unique constraint.
You could manually add a unique constraint with a query like this:
ALTER TABLE tb_contacts
ADD UNIQUE (Email)
By the way, the New Relationship dialog of the WebMatrix Databases workspace displays as possible references only the tables with suitable primary keys.
Note that your StarterSite db alterations inhibit the use of the WebSecurity class.
i would like to generate java classes using hibernate (Netbeans).
This seems not to be possible when the tables do not have a primary key.
I have no access to the database, i just found out, that they used sequences instead of primary keys.
create sequence SEQ_ANY
minvalue 0
maxvalue 99999999999999999999
start with 0
increment by 1000
cache 20;
So my Question is now, can i edit hibernate.hbm or hibernate.cfg.xml or any other file to tell hibernate which class contains a primary key?
f.e.
<table name="myTable">
<useThisFieldAsPrimaryKey name="uniqueSequenz"/>
</table>
Thank you!
A sequence is an objects to provide you with unique values for a primary may. Thus your table probably has a primary key which consists of a single column.
Then every table has a primary key (in the worst case all columns together form the primary key - which normally would be a bad database design). A primary key can have more than one column.
Hibernate likes primary keys which are made of a single column. Nevertheless you can define composite keys made of more than one column in Hibernate.
You need the schema of your table. If not you are stuck.
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).