Discover prisma table's primary key at runtime? - prisma

In prisma.io how do I find the name of a table's primary key? It is defined in file schema.prisma, but how to I identify the primary key of any arbitrary table at runtime, given the table's name?
So far I can find no reference to this in the documentation for Prisma Client. After instantiating PrismaClient I can find the list of tables and their column names (prisma._baseDmmf.datamodel.models), but no hint of which column(s) is the primary key.

I found an answer. I imported Prisma (in addition to PrismaClient) which provides access to Prisma.dmmf.datamodel.models which is an array of table models. Each table model includes an array of field definitions, each of which has the boolean property isId, which is true if that field is the row id.
For example:
Prisma.dmmf.datamodel.models[1].fields[0].isId
The names of each table and each field are string properties:
Prisma.dmmf.datamodel.models[1].name == 'Assets'
Prisma.dmmf.datamodel.models[1].fields[0].name == 'AssetId'
Hope this helps someone.

Related

Is it bad for columns in composite keys to have mismatching types?

Problem:
I'd like to make a composite primary key from columns id and user_id for a postgres database table. Column user_id is a foreign key with an integer type, whereas id is a string. Will this cause a conflict because the types are different?
Edit: Also, are there combinations of types that would cause problems?
Context:
I obviously should match the type of the User.id field for its foreign key. And, the id for my table will be derived from a uuid to prevent data leaks. So I would prefer not to change the types of either field I want in this table.
Research:
I am using sqlalchemy. Their documentation mentions how to create a composite primary key, but it doesn't discuss dealing with different types for each column.
No, this won't be a problem.
Your question seems to indicate that you think, the values of the indexed columns are somehow concatenated and then stored in the index as a single value. This is not the case. Each column value is stored independently but together. Similar to the way the column values are stored in the actual table.

How to have constraints in graph database such as orientDB?

I am coming from the RDBMS world. so forgive if I ask a badly phrased question.
I have a situation where I need to ensure unique or partial unique populating data inside cayley
In RDBMS such as postgres, I can build a table like this:
primary autoincrement key called id
foreignkey to person table called person_id
foreignkey to product table called product_id
foreignkey to price table called price_id
boolean field called is_removed
If i want a unique constraint such as the entire table can have a unique index such that product_id and price_id are together as a pair must be unique, I can do that.
if i want a partial unique constraint in postgres where if the is_removed is False, then the person_id, product_id, and price_id are unique.
Then if any of the foreignkeys are null, the constraints are not triggered.
How do I have something this inside a graph database such as orientDB?
My objective is to prevent creating illegal relations in the database
In orientDB you can define a schema for your database.
You have classes instead of tables.
Vertexes and edges are specialized classes.
You can define properties on classes, and define constraints on properties.
As a concrete example, the definition for the a User vertex class :
CREATE CLASS User EXTENDS V;
CREATE PROPERTY User.userId LONG;
CREATE PROPERTY User.description STRING;
CREATE PROPERTY User.screenName STRING;
CREATE PROPERTY User.lang STRING;
CREATE PROPERTY User.location STRING;
CREATE PROPERTY User.fetched BOOLEAN;
CREATE INDEX User.userId ON User(userId) UNIQUE_HASH_INDEX METADATA {ignoreNullValues: true};
CREATE INDEX User.description ON User(description) FULLTEXT ENGINE LUCENE METADATA {ignoreNullValues: true};
These are the links to the official part of the documentation about SQL and schema manipulation:
http://orientdb.com/docs/3.0.x/sql/SQL-Create-Class.html
http://orientdb.com/docs/3.0.x/sql/SQL-Create-Vertex.html
http://orientdb.com/docs/3.0.x/sql/SQL-Create-Edge.html
http://orientdb.com/docs/3.0.x/sql/SQL-Create-Index.html
http://orientdb.com/docs/3.0.x/sql/SQL-Create-Property.html

Database design with composite types in postgresql

How to refer another table built from composite type in my table.
I am trying to setup a database to understand postgresql and its Object oriented features.
The statement is as follows : There are multiple companies which can have board members.
Each company can own another company or a person can own that company too.
This is the type of database design I am looking for.
create type companyType(
name: VARCHAR,
boardMembers : personType[],
owns: companyType[]
)
create type personType(
name: VARCHAR,
owns: companyType[]
)
Create table company as companyType
Create table person as personType
I understand that I cannot self reference the companyType so I will probably move this another table.
My question is, when I am trying to insert into say company type, how do i insert list of person table objects as foreign key ?
Would making a column 'id' in each table and giving it type SERIAL work to use it as a foreign key?
That is not a relational database design, and you won't get happy with it.
Map each object to a table. The table columns are the attributes of the object. Add an artificial primary key (id bigint GENERATED ALWAYS AS IDENTITY). Don't use composite types or arrays.
Relationships are expressed like this:
If the relationship is one-to-many, add a foreign key to the "many' side.
If the relationship is many-to-many, add a "junction table" that has foreign keys to both tables. The primary key is the union of these foreign keys.
Normalize the resulting data model to remove redundancies.
Sprinkle with unique and check constraints as appropriate.
That way your queries will become simple, and you can use your database's features to make your life easier.

Is it possible to use DBIx::Class on a database without relationships?

I'm new to DBIC. I've imported data into a database. It's not possible to create relationships between the tables because, apparently, not all the values in the child table's foreign key column have a corresponding value in the parent table.
So is it possible to still do joins between the tables? I've skimmed through the tutorial and documentation but found nothing that addresses this problem.
You can of course define relationships in your DBIC schema that don't have a matching constraint in the database.
If you use $schema->deploy it will automatically generate constraints for all foreign key columns.

Composite key with user-supplied string column, foreign keys

Let's say I have the following table
TABLE subgroups (
group_id t_group_id NOT NULL REFERENCES groups(group_id),
subgroup_name t_subgroup_name NOT NULL,
more attributes ...
)
subgroup_name is UNIQUE to a group(group_id).
A group can have many subgroups.
The subgroup_names are user-supplied. (I would like to avoid using a subgroup_id column. subgroup_name has meaning in the model and is more than just a label, I am providing a list of predetermined names but allow a user to add his owns for flexibility).
This table has 2 levels of referencing child tables containing subgroup attributes (with many-to-one relations);
I would like to have a PRIMARY KEY on (group_id, upper(trim(subgroup_name)));
From what I know, postgres doesn't allow to use PRIMARY KEY/UNIQUE on a function.
IIRC, the relational model also requires columns to be used as stored.
CREATE UNIQUE INDEX ON subgroups (group_id, upper(trim(subgroup_name))); doesn't solve my problem
as other tables in my model will have FOREIGN KEYs pointing to those two columns.
I see two options.
Option A)
Store a cleaned up subgroup name in subgroup_name
Add an extra column called subgroup_name_raw that would contained the uncleaned string
Option B)
Create both a UNIQUE INDEX and PRIMARY KEY on my key pair. (seems like a huge waste)
Any insights?
Note: I'm using Postgres 9.2
Actually you can do a UNIQUE constraint on the output of a function. You can't do it in the table definition though. What you need to do is create a unique index after. So something like:
CREATE UNIQUE INDEX subgroups_ukey2 ON subgroups(group_id, upper(trim(subgroup_name)));
PostgreSQL has a number of absolutely amazing indexing capabilities, and the ability to create unique (and partial unique) indexes on function output is quite underrated.