C++: How to set GUID to NULL instead of GUID_NULL? - c++

I have a database which uses GUIDs instead of, say, an ordinary counter for the ID fields. But I can't seem to put NULL (instead of GUID_NULL) into such fields in DB even though. Yes, the field in the database does take NULL.
Let's say there is a parent-child relationship between two tables. So there is a parent and a child GUID references from one table to another. But the "root" parent does not have any parent and there I would like to be able to put NULL into its ParentUID database field. If I put GUID_NULL there then I will need a corresponding default row in the referenced table which has a GUID-value of GUID_NULL so that the foreign key constraint won't break.
Also, using GUID_NULL with default-rows at referenced tables will give me a resultset back when doing a standard join operation...which is not desirable.
They way it's done in code when inserting values into database is using a CCommand which takes structure that contains the values of the row fields to be inserted. One of these is a GUID type variable.
So it creates an SQL statement string looking like
INSERT INTO [tablename] (field1, field2, field3,...) VALUES(?,?,?,...)
and then in a loop there is something like:
command.field1 = 1;
command.field2 = 2;
command.GUIDField = ?????? //I want to put NULL here instead of GUID_NULL
command.Open(...);
I hope it is understandable what I wish to do and what the conditions in code are.
Thankful for help!
UPDATE:
Ok, it was very hard to exaplin correctly, but this is exactly what I want to do http://support.microsoft.com/kb/260900
Just that when I follow that example, it makes no difference...still I get FK constraint violation on insert so I suspect it is trying to insert GUID_NULL instead of NULL. :(

The link I had in my Update-section does work, my bad: http://support.microsoft.com/kb/260900
It is the answer to my problems, perhaps it will help someone else as well! :3

Related

how can I delete a row in Sqlite with foreign key?

I have a problem trying to delete a row from my db...
the DB looks like that:
[The DB picture]
I want do delete a user so I tried:
DELETE FROM USERS WHERE ID = 201;
but obviously it didn't work out at all because it connects with the other TABLES.
And I cant use DROP because its sqlite.
I look up on the internet and got nothing...
the error:
the error
Your table name is Users and You are using USERS in your command. It will give you error as It should be same as name of table.
DELETE FROM Users WHERE ID = 201;
Let me assume that you have another table that defines the user_id as a foreign key:
create table another (
another_id int,
user_id int,
foreign key (user_id) references users(user_id)
);
And you have data in this table, such as:
another_id user_id
1 200
2 201
3 201
Now, you want to delete 201 from users. What happens to rows 2 and 3 in this table? There are several options:
The rows remain with the values as is. But those values no longer refer to a valid user.
The rows that refer to 201 are deleted.
The rows are set to some value, such as NULL or a default value.
The default behavior without a foreign key constraint is (1). And you end up with dangling references. And your database lacks relational integrity. That is considered a bad thing.
SQL supports cascading delete and update foreign key references (although not all databases support these). These respectively implement (2) and (3) on the above list.
You can also manually change the referring rows so the user can be deleted. It is not clear what you really want to do, but this explains why you can't just delete the row and the facilities that SQL offers to get around that.

Is there any way around saving models that reference each other twice?

My issue is when saving new models that need to reference each other, not just using a related_name lookup, such as this:
class Many:
owner = models.ForeignKey('One')
class One:
current = models.OneToOneField('Many')
By default, these have null=False and, please correct me if I'm wrong, using these are impossible until I change one of the relationships:
current = models.OneToOneField('Many', null=True)
The reason is because you can't assign a model to a relationship unless its already saved. Otherwise resulting in ValueError: 'Cannot assign "<...>": "..." instance isn't saved in the database.'.
But now when I create a pair of these objects I need to save twice:
many = Many()
one = One()
one.save()
many.owner = one
many.save()
one.current = many
one.save()
Is this the right way to do it, or is there another way around saving twice?
There is no way around it, you need to save one of the objects twice anyway.
This is because, at the database level, you need to save an object to get its ID. There is no way to tell a sql database "save those 2 objects and assign the ids to those fields on the other object". So if you were to do it manually, you would INSERT the first object with NULL for the FK, get its ID back, INSERT the second object with the ID of the first one, get its ID back, then UPDATE the first object to set the FK.
You would encapsulate the whole thing in a transaction.
So what you're doing with the ORM is the closest you can get. You may want to add the following on top of that:
1) Use a transaction for the changes, like this:
from django.db import transaction
with transaction.atomic():
many, one = Many(), One()
one.save()
many.owner = one
many.save()
one.current = many
one.save(update_fields=['current']) # slight optimization here
2) Now this is encapsulated in a transaction, you would want to remove the null=True. But you cannot, as those are, unfortunately, checked immediately.
[edit: it appears Oracle might support deferring the NOT NULL check, so if you're using Oracle you can try dropping the null=True and it should work.]
You'll probably want to check how your code reacts if at a later point, when reading the db, if for some reason (manual editing, bugged insert somewhere, ...) one.current.owner != one.

Update Query Will Not Work Due To Key Violations

I am trying to update a specific field in the "Claims" table of my 2010 Access Database. I keep receiving an error message that says there are key violations. Here is the SQL:
UPDATE Claims SET Claims.LS_Name = "JPN"
WHERE (((Claims.Responsibility2)=0));
Is there any reason, based on the above code, that it is not working?
Thanks in advance!
My first guess would be that there is a unique key on Claims.LS_Name and your update hits more than one row.
OK, I ran into this issue as well in Microsoft Access and think I have ways to solve versions of this. The intermediate table may not be necessary. My experience is that autonumber is the issue so conversion to number seems to work, but you have to delete relationships for Access to allow this change.
remove all relationships to the destination table.
change from autonumber to number.
make new empty tablethatincrements starting with the next higher key https://superuser.com/questions/288087/how-do-i-set-the-first-value-of-autonumber-in-access
update/query to the tablethatincrements table instead of destination.
update/merge from tablethatincrementsto to the destination table, including new higher keys.
remake relationships
OR In certain situations. (for mine the new keys matched the old)
something like this (may not be exact steps)
remove all relationships to the destination table.
delete ID/primary key in destination
merge/update to destination
create new ID/Primary key in destination (so it can be autonumber to renumber keys)
remake relationships
Anyway my next research item is to see how to never use autoincrement and do the unique keys using other methods so relationships do not have to be removed to change from autoincrement to number.

How to create a foreign key for a non-key table field?

Simply put, i want to create a structure that has a component MAKTX, and to have a foreign key relation with MAKT-MAKTX.
More generally i want to have a foreign key check for a field that's not part of a primary key.
I see the button "Non-key-fields/candidates", but i don't really know how to use it.
Also, i don't want to use the "key fields of a text table" relation... but i don't know if that's relevant.
Is this even a good thing that i'm trying to do? I don't see any reason why it shouldn't be possible, but you might object.
[EDIT]: I have to mention that I don't really know what I'm doing. I really just want to fill a table i created with values from another, and to make sure that those values (namely MAKTX - kind ofvalues) in my table are always values from MAKT. Suppose i do the initial filling with a SELECT statement, i want the consistency to work even if i later insert new entries manually.
So I don't know whether this makes sense or not, it just sounds to me like a good idea to have the system perform this check automatically, if possible.
Main condition for creating foreign key relation is that the field should be a primary key in your reference table. While in the table you are creating foreign key its not necessary that the field is a primary key or not. The main reason for this is that foreign key cant be null.
Refer to below link for step by step process for creating foreign key relation in abap.
http://learnabaponline.blogspot.in/2013/04/how-to-create-table-in-abap.html
First off, I agree with vwegerts's comments, what you're trying to do doesn't seem to make any sense.
Perhaps this would make more sense: create your own table without the MAKTX field. Then create a database view, joining your table and the MAKT table (and set a default language in the selection conditions if you want to). This way you'll have the descriptions joined with your data, without duplicating the actual data (which is what it looks like you're trying to do).

Django - Insert Without Returning the Id of the Saved Object

Each time the save() method is called on a Django object, Django executes two queries one INSERT and one SELECT. In my case this is usefull except for some specific places where each query is expensive. Any ideas on how to sometimes state that no object needs to be returned - no SELECT needed.
Also I'm using django-mssql to connect to, this problem doesn't seem to exist on MySQL.
EDIT : A better explanation
h = Human()
h.name='John Foo'
print h.id # Returns None, No insert has been done therefore no id is available
h.save()
print h.id # Returns the ID, an insert has taken place and also a select statement to return the id
Sometimes I don't the need the retruning ID, just insert
40ins's answer was right, but probably it might have higher costs...
When django execustes a save(), it needed to be sure if the object is a new one or an existing one. So it hits the database to check if related objext exists. If yes, it executes an UPDATE, orherwise it executes an ISERT
Check documentatin from here...
You can use force_insert or force_update ,but that might be cause serious data integrity problems, like creating a duplicate entry instead of updating the existing one...
So, if you wish to use force , you must be sure whether it will be an INSERT or an UPDATE...
Try to use save() method with force_insert or force_update attributes. With this attributes django knows about record existence and don't make additional query.
The additional select is the django-mssql backend getting the identity value from the table to determine the ID that was just inserted. If this select is slow, then something is wrong with your SQL server/configuration because it is only doing SELECT CAST(IDENT_CURRENT(*table_name*) as bigint) call.