Android Question swap a record in a one to one relation

Cenny

Active Member
Licensed User
Hi,

I have two tables, 'Models' with a primary key Model_id and the second 'Devices' with primary key Device_id and foreign key Model_id.
The two tables have a one-to-one relation, that is to say that every model has only one device and that device can only be tied to one model.
The question is, how can I swap an existing device to another existing model?

Cenny
 

José J. Aguilar

Well-Known Member
Licensed User
Hi:

I don't know if I'm missing something, but you should change the foreign key Model_id in the Devices table to point another Model_Id.
 

Cenny

Active Member
Licensed User
exactly, but I cannot simply allocate the device to another model, because that model has already a relation to a device
 
Last edited:

Cenny

Active Member
Licensed User
I don't know how to do it, because, in my opinion I have to make two changes in one go
 

RB Smissaert

Well-Known Member
Licensed User
Hi,

I have two tables, 'Models' with a primary key Model_id and the second 'Devices' with primary key Device_id and foreign key Model_id.
The two tables have a one-to-one relation, that is to say that every model has only one device and that device can only be tied to one model.
The question is, how can I swap an existing device to another existing model?

Cenny
Read through this:

https://www.sqlite.org/foreignkeys.html#overview

RBS
 

emexes

Expert
Licensed User
How do you add new entries to the tables? Presumably the same issue should arise then, but there is some mechanism to work around it. Can the same workaround mechanism be used here?
 

emexes

Expert
Licensed User
thanks, but I was not that far yet
No worries (I think). Always good to spot potholes in the road ahead so you can go around them rather than through them :)

I would have expected that the database transaction would only enforce restrictions (like one-to-one-ness) at the end of the transaction, and look the other way regarding any interim temporary transgressions.

But all my B4A data stuff so far has just been straight text files, so I'm possibly making unwarranted assumptions here.
 
Top