Can OrmLite specify foreign key to an attribute other than the primary key - servicestack

I have a table structure where a foreign key from Employee table references something other than the primary key of the Department table. This is for historical reasons, so that's just the way it is.
This works: q.Join<Department>((e, d) => e.DepartmentId == d.DepNo);. Note that Department's primary key is Id.
Now, is there any way I could specify the join relationship (with the column name)? The reason is that I'd like to use AutoQuery's built-in IJoin<Employee,Department> thing, but that doesn't let me specify the columns to use.

Please see docs on Reference Conventions for how to define implicit PK and FK references.
AutoQuery only supports implicit references, if you need more customized behavior you’ll need to create a custom AutoQuery implementation.

Related

How do i implement friendship functionality in database using sequelize for social media demo app

I'm building a social media demo app and to add friends functionality to that app.
In database I am thinking about making new table(model in sequelize) which contains user1 id and user2 id and a status with one character (accepted, rejected etc).
I tried implementing it but faced some issue: Now i need to have unique key but for combination of user 1 and user 2 (pair of user1 and user2 id).
How do I implement something like this using sequelize and also I don't think this is the best way to do it, so if you have a better way please let me know, thanks.
Your description is somewhat lacking and missing some components. However it is clear enough to see what you are describing
is a many-to-many (m:m) relationship. It is just that the both sides come from the same table. While not the most common relationship,
neither is it too unusual. Further the resolution is the same as any other m:m. Create another table that has the key to both sides
as foreign keys. There is one slightly unusual twist in that the combination (parent1, parent2)(P1,P2) and the combination (P2,P1)
should be considered the same. This introduces a slight twist to the norm; instead of generating a PK from the the parent columns
the PK will be just be sequence generated (technically the PK is not required unless there are anticipated child tables of it).
We then create a unique index on the ordered pair (P1,P2). So something along the line of:
create table friends( f_id integer generated always as identity
, user_id_1 integer not null
, user_id_2 integer not null
, status varchar(1) not null default 'P'
, friends_since date not null default now()::date
, constraint friends_pk primary key(f_id)
, constraint f2user_1_fk foreign key(user_id_1)
references users(user_id)
, constraint f2user_2_fk foreign key(user_id_2)
references users(user_id)
, constraint cannot_friend_self
check (user_id_1 != user_id_2)
);
--
-- unique index to recognize (P1,P2) = (P2,P1)
create unique index already_friends on friends
( least(user_id_1,user_id_2), greatest(user_id_1,user_id_2) );
See fiddle here for examples. The bi-directional nature of Friends table causes additional complications within the queries. Typically I hide complication within SQL functions. I have also added a couple useful functions. You should be able to uses these as examples. Note, since they are SQL functions you can extract the statement and convert to a parameterized query.
Sorry, I do not know sequelize so I will not guess at the translation, but there seem to be many examples of the internet for all the above.

Name the primary key of Container in Cosmbos DB Sql Api different than "id"

I am working on Azure and I have created a database with Cosmos DB SQL API. After creating a container I see that the primary key is always named "id". Is there any way to create a container with PK with name different than "id"?
Every document has a unique id called id. This cannot be altered. If you don't set a value, it is assigned a GUID.
When using methods such as ReadDocument() (or equivalent, based on the SDK) for direct reads instead of queries, a document's id property must be specified.
Now, as far as partitioning goes, you can choose any property you want to use as the partition key.
And if you have additional domain-specific identifiers (maybe a part number), you can always store those in their own properties as well. In this example though, just remember that, while you can query for documents containing a specific part number, you can only do a direct-read via id. If direct reads will be the majority of your read operations, then it's worth considering the use of id to store such a value.
PK = primary key, by the way ;) It's the "main" key for the record, it is not private :)
And no, as far as I know you cannot change the primary key name. It is always "id". Specifically it must be in lowercase, "Id" is not accepted.

Can we add primary key to collection datatypes?

When I tried to retrieve table using contains keyword it prompts "Cannot use CONTAINS relation on non collection column col1" but when I tried to create table using
CREATE TABLE test (id int,address map<text, int>,mail list<text>,phone set<int>,primary key (id,address,mail,phone));
it prompts "Invalid collection type for PRIMARY KEY component phone"
One of the basics in Cassandra is that you can't modify primary keys. Always keep that in mind.
You can't use a collection as primary key unless it is frozen, meaning you can't modify it.
This will work
CREATE TABLE test (id int,address frozen<map<text, int>>,mail frozen<list<text>>,phone frozen<set<int>>,primary key (id,address,mail,phone));;
However, I think you should take a look at this document: http://www.datastax.com/dev/blog/cql-in-2-1
You can put secondary indexes on collections after cql 2.1. You may want to use that functionality.

Default primary key setter and getter generated by Service Builder

I'm working with Liferay. When create an entity, we define one column as the primary key. Then I look at the entity model, I see some methods setPrimaryKey, getPrimaryKey as well as the setter and getter of the column that we defined as the primary key before. What is the difference of these methods? Do we need to use both methods once we add new entity into the table or just one
Assuming that your project is built by many people. Some one designs entity and you only code view page. When you want to get or set primary key of entity but you don't know what column. If don't have setPrimaryKey or getPrimaryKey methods, do you look up in the code to find what column is primary key?

Creating associations in EDM model on unique keys

I'm developing a new application which is based on a legacy database. The old legacy database does not use reference integrity and first we try to not change the existing schema. I still want to be be able to use navigation properties in my EF generated POCO classes.
However, the old data model has one big issue regarding child-parent relationships: the column in the child table is referencing the parent table via the uniqe key of the parent, not via the primary key of the parent table. Is it still possible to generate such associations?
I tried to mark the unique key in the parent table as "entity key" but then I still need to provide a mapping for the parent table's primary key which I am not able to because there is no mapping for it available, this primary is just a dummy "counter". If I do not provide a mapping for the primary key, I get
"Error 111: Properties referred by the Principal Role XXX must be exactly identical to the key of the EntityType YYY referred to by the Principal Role in the relationship constraint for Relationship ZZZ. Make sure all the key properties are specified in the Principal Role"
No EF does not support unique keys at all. It is hopefully planned feature of the next major release.

Resources