I have a SQLAlchemy many-to-many relationship -- I want this relationship to be set-like.
That is, the association table should not have duplicate values. I have a uniqueness constraint on the MySQL server to accomplish this. The number of items in the relationship is large, so the relationship is configured as dynamic.
The problem is, I would still like to be able to use the built-in instrumentation to add items to the relationship. However, this results in "INSERT" statements being emitted for the relationship. I would prefer them to be "INSERT IGNORE" statements, since I don't want to have to load all of the items to determine whether or not they currently exist.
Has anybody ever dealt with this? Does anybody know of a way to ask SQLAlchemy to emit INSERT IGNORE statements for a dynamic relationship?
You can use prefix_with() function, available in 0.7.7 version of SqlAlchemy.
Related
I was unable to find a clear answer of how to create an IS_A relationship in Access.
There was the same question here, but without a concise answer:
IS_A relationship primary key validation rules
I have the entity Employee, and two sub-entities Loan_Officer and Branch_Manager. It's a school example of an IS_A relationship really.
I've managed to create A relationship, but there needs to be a constraint that an employee must be either a Loan Officer or a Branch Manager, but can not be both. Now, I can't figure out how to do this, because what ever I do, I can assign the same Employee_ID in both sub-entity tables at once.
I've connected the tables via the PK, as it's shown here:
Now, this table design is just something I've done, in order to be able to connect them via a one-to-one relationship. I had to set the PK of Loan_Officer to "Number" and not "AutoNumber", in order to be able to connect them. The other option is to have a separate PK in Loan_Officer, like "Loan_Officer_ID", and a foreign key, "Employee_ID" in the Loan_Officer table, but the results are again the same (also according to the ER Diagram, the sub-entities don't have a separate PK).
You can't. This is not a feature of the Access database.
You can create CHECK constraints to check for such conditions, but those don't offer features to cascade operations.
See this answer for an example on how to create a CHECK constraint.
There is no such thing as an 'Is A' relationship in databases between tables. This is instead a field in the Employee table or Employee History Table.
The issue of 'can't be both' is a matter of validation logic. Where this validation logic is applied is probably at the form object level (during data entry), not the table level (no data should ever be entered directly into tables by end users).
Look into Access Data Macros . They can be used like SQL triggers firing off when a record is INSERTed, UPDATEed, DELETEed etc.
I'm working with a big mySQL database in IntelliJ.
I'm trying to join two tables which aren't in a direct key-relation to each other. Hence I have to join over multiple tables.
Because I barley know the database scheme, I can't find out these tables in a appropriate time. I know that IntelliJ has a function which can visualize all tables with their relations within the database but does it also provide a function where I can find out all tables in between two specific tables?
You can get help from Intellij partially for your task, using the visualization feature as follows-
The relationship between tables are clearly shown.
For your question, you need to check the primary and foreign keys for each table which are easy to know as they are highlighted.
Traversing them you can find the relationship.
I want to create 3 tables:
categories
items
items within categories
The categories and items records need to have unique keys, so I had planned to create them with an auto-increment integer field called RecordID.
The "items within categories" records will basically have the following fields to match items to categories: item_RecordID, category_RecordID.
However, I want to be able to rebuild these tables on multiple installations through flexible import/export utilities that I build myself. I don't trust auto-increment to keep the RecordID fields the same through multiple installations.
What is the best way to maintain consistency of the RecordID fields? Should I create a duplicate RecordID field as I write each items and categories record, then use that duplicate in the "items within categories" table? Or is there a more straightforward or more efficient way to do this?
This is a simple question. I apologize if I got any terminology wrong - I am relatively new to MySQL and php. (Background is Microsoft Access, ASP, and ecommerce technologies that already have import/export set up.)
Thanks for any help.
What you need is FOREIGN KEYs. Whithout it you cannot trust on consistency on multiple tables and especially on AUTO INCREMENTs. A-I can sometimes change in some situations (if you play with backup/delete/restore or replication).
I am trying to delete an object that sometimes has a many-to-many relation. The code I use now is:
db.DeleteObject(registeredDevice);
db.SaveChanges();
This ofcourse just removes the registeredDevice. But usually this device has a many-to-many relation to a project in the database. When trying to delete the device in that scenario it will give an error.
The DELETE statement conflicted with the REFERENCE constraint
What I need to do is remove the device and its relation (the entry in the many-to-many table, not the project it is related to). How do In do this with LINQ ?
To remove everything, you need to make sure all the data is loaded. Basically, linq to sql has to know about the data it is deleting before hand to make the best judgement on how to delete it.
Hoep that helps
I am trying to build an Entity Framework model for my database schema using EF4 and Visual Studio 2010. Since I am stuck using MySQL as our database (for now), I pretty quickly discovered that since MYISAM tables don't support foreign key constraints, the designer does not have any way to automatically create all the associations for you. It is easy enough to go through the model and build them all manually, but I ran into a problem trying to map a pure join table.
Normally if you are using SQL Server or some other database that supports Foreign Keys, the designer will create all the mappings for you and in the case of pure join tables will create an AssociationSetMapping such that the join table is entirely hidden in the model. Instead you end up with a Many to Many mapping between two two primary entities.
However I am at a loss as to how to create this kind of mapping manually? If I create a Many to Many association between my two entities, I end up with a regular Association, not an AssociationSetMapping. There does not appear to be a way to create one of these in the designer than I can figure out, and to tell it which join table is supposed to be used at the database level.
Am I missing something, and there is a way to do this?
If not, I suppose I have to hack the XML directly, but at this point it is not clear what I need to add to the XML files and where, in order to manually create the association set mapping?
Ok, I can answer this one myself. Once you create a many to many association with the designer, you DON'T set a referential constraint on it, but rather use the Mapping Details window to tell the association what table is the join table, and which keys map to which fields in the database. Simple enough once I figured it out :) But I could not find any good documentation on this.