Generate random unique number using entity framework single database call - entity-framework

I am supposed to generate a partial random unique id to be stored as an identifier for users.
Criteria:
8 digits
First 4 digits is of my own (for eg. the year)
Last 4 digits can be anything random.
How do I use entity framework to make sure this id is unique? I don't want to have a loop that generates then check the database. Can something like this be done in 1 database call?

The only way to do this in a single call would be to call a stored procedure that generates the ID and checks uniqueness.

Related

Customized ID in PostgreSQL using the user's input

I need to create an ID that uses data from the user's input. For example, the first letter is "M" the second is the year of birth that the user inputs, then the two first characters of the province the user lives in (for example, ON for Ontario), and then a sequence of random numbers. Like this: M-1990-ON-0001
Do you have any idea how I can do that?
You can do that as a generated column. ie:
customId text GENERATED ALWAYS AS ('M-'||extract(year from birthdate)::char(4)||'-'||province) STORED
However, you should never make such info a primary key. Just use a key for yourself but do not as a primary key.
DBFiddle demo
If you think about it such a value wouldn't be even unique (same province, same year, two persons for example).

How can I do a read-create transaction with Npgsql?

In an ASP.NET Core 6 website which uses EF Core (Npgsql) I need to do the following scenario.
There is a table which holds users' data of some type. Each row of this table (per user) has a calculated column which uses the previous row's value. This opens up the race condition problem. I need to read the latest row (for a user), then create the new row and then insert it into DB. And during this time I need an assurance that the created row is the last row and no two rows are created at the same time.
How can I achieve this with EF Core?
I have read about Postgresql's table locks which are not supported in EF and slow down the app anyway (not recommended). Another thing that I read about is transactions, but every example I've seen uses it for updating or creating multiple rows, I don't know if this scenario can be handled with transactions.
One simple way to model this, is to have a composite key consisting of the user ID and a user row ID, which the application will increment client-side. In this model, your application simply loads the latest row, and then attempts to insert a new row for that user with an incremented value. If two threads attempt to do this concurrently, one will get a unique index violation, and can retry again or whatever.
So let's assume there's user id 4, and their latest row has user row id 10. You code loads that, and then attempts to insert a new row with user id 4 and user row id 11. Since there's a composite key defined over the two columns, only one such insert can succeed.
This should work just fine in EF.

FileMaker - Getting Data From Another Table with Multiple Field Restrictions

I can't think of a better title, so feel free to make a suggestion once you understand the issue.
I was given a table to work with that I need to call from another table:
Name
Month
Type
Value
For each record in the main table I need to pull one "Value" that corresponds to it. What it is will be determined by all three of the other fields. So for example, if a record in the main table is:
Name:
Google
Date:
3\17\2016
Type:
M
Then I need to pull the value for the record in the other table where the Name is "Google", the month is "3", and the type is "M".
I was able to do this successfully (if slowly) using an ExecuteSQL command in a calculation field, with a ton of nested If statements for the names (I have yet to figure out how to input the record's data directly into the ExecuteSQL statement, it breaks when I try). I would prefer to just grab the data directly. I can't switch over to the other layout because I need to see all of the records at once. I can't do a simple relationship because there isn't a real relationship, it's like there are three foreign keys working in tandem and I only know how to use one to call the data.
Any idea on how to do this more simplistically?
Some ideas I've had but not sure if it will work:
Using a calculation field as a related field to dynamically point to the row by code (concatenate the three relevant fields into a type of code). Not sure if you can connect two tables by a calculation field.
Doing that same thing when calling the data into the table in the first place, adding a code to create a single primary key.
Here are my relationships:
I can't do a simple relationship because there isn't a real
relationship, it's like there are three foreign keys working in tandem
and I only know how to use one to call the data.
Simply define a relationship with three predicates - i.e. three pairs of match fields.

Postgres how can I make 2 numerical fields unique to each other

I am trying to do a following/follower type of concept that many social networks have. I have 2 INT fields called me and following, Those INT fields are composed of users Unique ID. I want to make so that if user 23 is following user 7 or 23->7 then user 23 can not follow user 7 again because the relationship already exists. This picture will clear things up
Notice above the first 2 rows are 31->27 or user 31 is following 27 twice that is redundant. Is there some constraint that I can use to prevent that from happening ? I am using postgres version 9.4
You can do this by creating a unique index. But not just any unique index, one on expressions:
create unique index unq_t_me_following
on (least(me, following), greatest(me, following));

How can I get a Random quote (text) from a database (CoreData) of quotes? (iPhone)

I want to develop an app that gives a random quote every time you change the page. And I want to put like 1000 quotes in the database. How is the best way I can get a Random Quote? arc4random?
To extend the answer you were given on selecting a method to generate a random number - choose a random number method, and have an index for each quote in the DB so that you can query a quote directly from the random number you compute.
Also you can get counts directly from Core Data (without doing a real query) to figure out the range of random numbers you want to ask for.