how to find records that do not have a value in the foreign key column in extbase - typo3

In extbase, how can I find all records that do not have a value in the foreign link column.
I have a model that can relate to a different object. The relation is 1:n and it is called "dayend".
I now create a query like this in the responding repository:
$query->equals("dayend", null);
This does not work, meaning that constraint is completely ignored. The original value in that column is "0". Thus, I also tried
$query->equals("dayend.uid", 0);
However, this throws an error: "Could not determine type of relation."
My goal is to get all objects that are NOT related to any "dayend" object. Any ideas?

Related

can not remove circular dependent objects in Entity Framework

I have two tables have circular reference to each other (you can see the FK and PK i nthe image)
(In otherprotectionItems table , I have a column from Document , and that record in document is related to otherprotectionItem table ad a foreign key. the first relation is NO ACTION in delete and the second relation is CASCADE on delete)
I have this issue when I want to delete a row in otherprotectionItem table.
Error :
Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.
Just wanto to note , I have this solution. but I prefer to have a better . Otherwise this is the answer.
I will remove the documents manually , save change in DB . and then remove the OtherprotectionItem table record
Data.Util.DocumentFinder.FindRecursive(item)
.ToList()
.ForEach(d => db.Entry(d).State = System.Data.Entity.EntityState.Deleted);
db.SaveChanges();
db.OtherProtectionItems.Remove(item);
db.SaveChanges();
Note : this FindRecursive is a function I create and it iterates all documents and find all documents related to this OtherprotectionItem record

EF query contains incorrect elements

I have a query with EF which looks like this:
var x = _db.qMetaDataLookups.ToList();
if I execute, direct on the SQL server SELECT * FROM qMetaDataLookup, 2155 distinct rows are returned. After executing the above, x ALSO contains 2155 elements.
The problem is that the data is wrong. I'm not getting the same data back from the EF as I do from the SQL Query.
In particular, theres a particular element that exists on the SQL output, call it "WXYZ", which makes no appearance at all in the EF version of the query (against the exact same database).
Instead, what I find are numerous repeats. If I call x.Distinct() the list filters down from 2155 elements, to a mere 143.
I'm flummoxed. I have never seen my EF and SQL results differ on a query this simple. There must be a very simple [face-palm] explanation, but I'm missing it.
Thanks.
EDIT qMetaDataLookup (a view) are contains information about our database. In essence, its a listing of all tables and views, and each of their columns, with other information about the datatype, length, precision, scale, etc. The 'key' in this table ought to be the column that matches "tableName.columnName" but instead EF chose for it all the datatype properties. This is why the query fails to perform as desired.
Make sure the entity key is set correctly for qMetaDataLookup in the Entity Data Model. Sometimes the entity keys are messed up...
The issue might have been that your model was using a key with duplicate values where the Entity Framework was expecting unique values. This would happen if, for example, your data model used a composite primary key composed of foreign keys from other tables. It seems EF doesn't like composite primary keys very much, and so returned results from queries will generate what appear to be duplicated rows.
The fix seems to be to add a surrogate primary key column to your table which is guaranteed to be unique. If you still need to reference the foreign columns that's fine, so long as they aren't being used as a composite primary key for the table.
I can't claim any credit for the solution, but here's the link that helped me solve my issue:
http://jepsonsblog.blogspot.ca/2011/11/enitity-framework-duplicate-rows-in.html

EF Error - Unable to update the EntitySet because it has a DefiningQuery

I have removed the PrimaryKey from my table, refreshed the EDMX, and now I am getting this error message when doing db.SaveChanges():
Unable to update the EntitySet 'Results' because it has a DefiningQuery and no element exists in the element to support the current operation.
Now...
1 - I dont want PrimaryKey in my table. The table is just a bag of values, no PK is required.
2 - I read another post where someone suggested to remove DefiningQuery element from EF generated EDMX. It is not working, and I avoid manual changes to automatically generated EDMX.
Any idea how I can avoid this error, and not define PK in my table?
Thanks.
In Entity Framework everything must have a key, at least in the model. You can remove it from the database but still you'll have to define a key in the model, which may be a composite key to ensure it is unique. Otherwise EF won't be able to materialize or save objects correctly.
However, I would use a primary key in the table anyway. It is no trouble at all to have an identity field in it and you don't have to worry about finding a unique combination of fields yourself. I can't imagine that the table does not need some notion of identity, or can it really have two exactly identical rows?

Cannot insert NULL into a non-identity column in Entity Framework. Funny thing... it's not null?

I have a SalesOrder table with columns for ID and OrderID. ID is an auto-generated int. OrderID is a non-nullable string with a max length of 20, and we use it to store the customer's order number for reference.
After adding my new SalesOrder and calling SaveChanges, I get the following error:
Cannot insert the value NULL into column 'OrderID', table 'SalesOrder'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Problem is, the object that I'm saving actually does have an OrderID! It's almost like it's trying to save the entity first before it saves all the values. Is this how EF handles things?
My setup is EF4.1, using an EDMX model-first approach. StoreGeneratedPattern is set to None. Default Value is currently set to (None) but I've tried various values. Entity Key is False, since it's not part of the key. I've also tried deleting the SalesOrder entity and regenerating it from the database.
I would also like to see your code...I had similar problems when filling objects in a loop then saving them with savechanges. I thought all the fields were populated, but they were not.
I'd have to see your code that executes before the save changes before I can offer anything really helpful.
If your problem is like mine and you are calling savechanges after using an iterator to populate your objects, then you can find the bad data by moving savechanges into the iterator so that it is called with each iteration...but this is all hypothetical guesswork without seeing your code...

Setting Entity Framework Fields

I generated a Members table and a MembersType table which has a primary key which links to the Type foreign key in the Members table. The MembersType table is literally just 3 records, so that each Member can be of MemberType 1, 2 or 3.
Now the problem is that when Entity Framework generates the data layer and objects for the Members object, it creates a MemberType object in the Members object, but all I want to be able to do when setting it is:
Members.MemberType = 1;
but because of the above, I have to do this:
MemberTypes = db.MemberTypes.Where(x => x.MemberTypeId == 1).AsQueryable().First()
Is there anyway to stop it from generating an object on foreign keys so I can just set it as an int? Surely this is more quicker and resource efficient than querying the type table everytime too.
You have encountered one of everyone's least favorite features of EFv1. The problem is that everything is an Entity, so you can't get to foreign key values as primitives.
Your code sample shows how it has to be done in EFv1. The best you can do is cache those enum values up front so you don't have to keep getting them from the context. EFv4 does away with this restriction with "FK Properties," which is just a fancy way of saying raw foreign keys you can set directly.