Postgres, Relationship and Composite key #2765
Replies: 6 comments 1 reply
-
Another example why I need composite keys: I have 2 tables that relates to a third table using the same columns The third table:
The product sale table:
The service order table:
|
Beta Was this translation helpful? Give feedback.
-
Hi people, Since I want to help with this, I started looking at the lucid src. Meanwhile I found a function const primaryKeyColumn = modelConstructor.$keys.attributesToColumns.get(
modelConstructor.primaryKey,
modelConstructor.primaryKey
)
/**
* Returning insert query for the inserts
*/
if (action === 'insert') {
const insertQuery = client.insertQuery().table(modelConstructor.table)
insertQuery.returning(primaryKeyColumn)
return insertQuery
} And it made me reflect on:
I still wouln'd be able to use |
Beta Was this translation helpful? Give feedback.
-
Composite keys is not Lucid does not support and requires a lot of internal changes to make it work properly, especially with relationships. I personally do not have the mental bandwidth right now to even go through every single scenario and make sure it works fine with composite keys. So, I will prefer to skip this altogether for now. |
Beta Was this translation helpful? Give feedback.
-
As Virk says, Lucid is not handling composite keys any time soon, so lets discuss how to handle it using Lucid without relationships: What you think is the best way of doing that?
I stiil need to test around to make my opinion.
|
Beta Was this translation helpful? Give feedback.
-
Hi people, So I think I solve a part of my issues by extends BaseModel and overriding the export class MyBaseModel extends BaseModel {
protected static compositeKeys: string[] = [];
public $compositeKeyValue(this): any | undefined {
const modelConstructor = this.constructor as typeof MyBaseModel;
const getValue = (key) => {
const column = modelConstructor.$getColumn(key);
if (column && column.hasGetter) {
return this[key];
}
return this.$getAttribute(key);
};
return modelConstructor.compositeKeys.reduce((obj, key) => {
obj[key] = getValue(key);
return obj;
}, {});
}
$getQueryFor(
action: "insert" | "update" | "delete" | "refresh",
client: QueryClientContract): any
{
const modelConstructor = this.constructor as typeof MyBaseModel;
const primaryKeysColumns = modelConstructor.compositeKeys.map((key) =>
modelConstructor.$keys.attributesToColumns.resolve(key)
);
/** Returning insert query for the inserts */
if (action === "insert") {
const insertQuery = client.insertQuery()
.table(modelConstructor.table)
.returning(primaryKeysColumns);
return insertQuery;
}
/** Returning generic query builder for rest of the queries */
const query = client.modelQuery(modelConstructor);
const compositeKeysValues = this.$compositeKeyValue();
for (const key in compositeKeysValues) {
const column = modelConstructor.$keys.attributesToColumns.resolve(key);
query.where(column, compositeKeysValues[key]);
}
return query;
}
} With this I can create the relationship between I could make compositeKeys concatenate the primary key and I think that would make my code work whether the table uses a primary key or not. |
Beta Was this translation helpful? Give feedback.
-
Using primary key(id, store_id) is a bad idea. This will make many queries more complicated and more prone to error. It sounds like what you are really trying to make is a many-to-many relationship between stores and offers. If this is the case you should have a store table with unique store_id as a primary key, an offer table with unique offer_id as a primary key and a store_offer table that has a primary key of store_id and offer_id.. . |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to model the existing Postgres database through Lucid, and that database has some tables with composite primary keys. I know Lucid does not handle composite keys for now so I'm loading/creating manualy.
My solution
createMany
static function, usingDatabase
queries directly, return all its columns and then creating the model instance.My problems with that:
pg
) needs to return a columnid
, which does not exist, that why I usingDatabase
directlyMy Question
There is a way to use relationships?
If not, how can I help create a way?
The Code example
On the Owner Model
On The Owned Model
this function
findByPedidoEOrdemDoItem
returns a list of components of an item in an order(pedido).The primary key would be
Beta Was this translation helpful? Give feedback.
All reactions