I'm trying to reflect and automap some tables in sqlalchemy that have composite primary keys from an Oracle db. When I run
metadata = MetaData()
metadata.reflect(bind=engine, schema='USER')
Base = automap_base(metadata=metadata)
Base.prepare()
print(list(Base.classes))
I get a list of the tables that have primary keys in the database, and not the ones that don't. I know that you can manually map tables with composite primary keys (http://docs.sqlalchemy.org/en/latest/faq/ormconfiguration.html), but is there a way to feed the two keys that make up the composite primary key to the reflection for automapping?
Related
Just as the above question. Besides, this table's primary key is a foreign key in another tables, so what's the difference if we map them to their original tables without passing through this table's composite primary key?
1) Without an integer primary key, performance will suffer for many tasks.
2) Many tools that automate code generation, require an integer primary key.
I am using Fluent NHibernate (which I am fairly new to) in an application I am developing using a legacy Oracle DB. The DB has composite keys which are comprised of foreign keys and database generated columns. The generated columns are supplied by calling a DB function with the table name, and one of the other foreign key parts. The generated composite key parts are not unique, and I cannot change this. The generated key parts are often used as foreign keys on other tables too.
If I create entity mapping which specifies the composite key as it is in the database, then we cannot use any identity generation strategies, which breaks unit of work
If I create entity mapping which specifies only the generated column as the primary key, then I can use trigger-identity to generate the ids, and I get unit of work, but I then have a problem when I want to update, or access a child collection: The other parts of the key are not included in the WHERE statement.
Can anyone give me any advice on how to proceed?
If I stick with mapping composite keys, can I extend nhibernate to output the SQL to use trigger-identity? If so, can you suggest a starting point?
If I map a single column key, can I include other properties in a WHERE clause for HasMany mapping and Updates?
Unfortunately, as you have already found out, there is no support at all for this setup.
My suggestion is to do INSERTS manually (using custom SQL, for example). And yes, this breaks the UoW, but that is true of identity too.
I have a table that has no primary key, and one cannot be created either. I can construct a unique key using three columns of this table. Now hibernate demands an id for every annotated class, how do i satisfy this id with the unique Id I can create.
If it is an entity type then you should use a composite key. This can be done by moving primary key fields to separate class and mapping that in entity class with an #Id annotation.
See Mapping composite primary keys and foreign keys to composite primary keys
If it is not an entity but a value type you should map it accordingly.
See https://stackoverflow.com/a/1696146/324900 and Entity and value types in hibernate
I am working on a database whose tables won't have any foreign key constraints except composite primary keys for some of the tables. Is it possible to map the database using LINQ to SQL and then set the foreign key constraints in the DataContext being generated?
Thanks
Yes it is possible.
If they don't exist you just have to create them manually in the designer - use the Inheritance tool from the designer view and drag from the primary key on one table to the foreign key on the other.
Important notes:
1) both tables must have a primary key defined: see my blog entry on this point
2) the datatypes of the two columns must match - an integer cannot join to a date
I have two tables DATA_PERSONELL which has a primary key of PERSONNEL_ID, and SYS_USER_INFO which has a primary key of PERSONELL_ID which is also a foreign key to the DATA_PERSONELL table.
When attempting to map SYS_USER_INFO, the only mapping that I've tried which has worked is to map SYS_USER_INFO.PERSONELL_ID as an integer, which ultimately doesn't provide access to the data in DATA_PERSONELL.
Does anyone know how to do this the right way?
Use a one-to-one mapping.
More details here :
http://ayende.com/blog/3960/nhibernate-mapping-one-to-one
http://sdesmedt.wordpress.com/2006/07/24/nhibernate-part-3-mapping-techniques-for-aggregation-one-to-one-mapping/