Hi :)
I have 2 tables
Client(client_id(pk), first_name, last_name, user_id(fk))
User(user_id(pk), password, client_id(fk)). (my database is more complex, this is just an exemple).
And I'm trying to insert a new record in my database, but i just keep getting an error from the foreign key, because of the one to one relationship. I've tried to use a default value for user_id in client, but the same error. Does anybody know how to solve this pb? please :(
Loading


Javeed M ShaikhRank #839 · 2.7K reputationPosted Apr 9, 2013, 12:16 PM EDT
I think it should be
Client
client_id (PK)
first_name
last_name
User
User_id (PK)
client_id (FK)
password
Insert in the Client table first, and use the client_id to populate the User table.
Violeta Popa132 reputationPosted Apr 9, 2013, 12:28 PM EDT
Javeed M ShaikhRank #839 · 2.7K reputationPosted Apr 9, 2013, 12:34 PM EDT
to answer your question you can use the
SELECT SCOPE_IDENTITY() or @@IDENTITY
to get the last identity value in your scope and use that value to populate the child table.
Vishal GilbileRank #268 · 9.4K reputationPosted Apr 9, 2013, 8:44 PM EDT
Hope that solves your problem.
With Regards,
Vishal Gilbile