TypeORM Postgres - there is no unique constraint matching given keys for referenced table - postgresql

I am getting the error:
there is no unique constraint matching given keys for referenced table
My models are as follows:
#Entity()
export class User {
#PrimaryGeneratedColumn('uuid')
userId: string
#OneToMany(() => Position, position => position.user)
positions: Position[]
}
#Entity()
export class Position {
#PrimaryGeneratedColumn()
positionId: number
#ManyToOne(() => User, user => user.positions)
user: User
}
It seems like using the UUID strategy might be the error? But I also noted that the generated query defines the userID column with PK contraint:
ALTER TABLE "user" ADD CONSTRAINT "PK_d72ea127f30e21753c9e229891e" PRIMARY KEY ("userId")
Any help is very much appreciated.

In the end I realise there is some polluted migration files inside of /dist that was messing with the database. After deleting /dist and having all files regenerated, the problem was gone.
Polluted /dist is apparently a problem not well looked into and can affect tsnode-dev, typescript, nodemon, and nestjs dev instances that generates /dist on the fly and incrementally.

Related

Update a model and its relations with already existing primary keys

I have the following struct, with LinguisticMetadata being a relation:
metadataDB := &dbm.VideoMetadata{
ID: *imdbid,
ImdbID: *imdbid,
TmdbID: tmdbID,
IsSeries: input.Episode != nil,
ImdbRating: imdbRating,
Adult: extendedMetadata.Adult,
LinguisticMetadata: linguisticListDB, // LinguisticMetadata []*LinguisticMetadata `gorm:"not null"`
Subtitles: subtitlesDB, // Subtitles []*Subtitle
}
I want to create a new metadata, or update the existing one if it already exists. This is not an issue and can be achieved easily like so:
newMetadata := dbm.VideoMetadata{}
db.Where(dbm.VideoMetadata{ID: *imdbid}).Assign(metadataDB).FirstOrCreate(&newMetadata)
However, I also need to have the same behavior for its relations. The primary keys for LinguisticMetadata is the prefix of a country, eg: "en", "fr", "it"... So it is very likely that it already exists for the given metadata. Hence, if the primary keys of the relations already exist, the model should be updated (or replaced).
With the code shown above, I get the following error if the LinguisticMetadata primary key already exists:
pq: duplicate key value violates unique constraint "linguistic_metadata_pkey"
This does not make sense to me, because as stated here:
GORM will auto save associations and its reference when creating/updating a record. if association has a primary key, GORM will call Update to save it, otherwise it will be created.

Implementing 3 way relationship in Prisma

I also asked this question on Prisma forum.
** EDIT **: The forum has since been locked as read-only just 1 day after my question. It's sure getting scary because there is no official announcement of whether they'll stop developing Prisma 1 after implementing promised features or not. Surely they could have said something. And TBH, the unstable Prisma site does add to my shaking confidence in Prisma despite the number of stars in their repo.
I'm new to Prisma. So I have a 3-way relationship between User, Event and Role I would like to define. For each association of a User and an Event, there exists a Role for that association. If I were to design a database table for another ORM, I would have created a event_user table with user_id, event_id and role_id as columns to relate them.
Practically, a row of these 3 columns must be unique. Obviously, it would be good if Prisma can do the safeguarding of these constraints, but the obvious solution I see might not even come to Prisma 1.
My current design consists of the following:
type User {
// ...
eventUsers: [EventUser!]!
}
type Event {
// ...
eventUsers: [EventUser!]!
}
type EventUser {
role: Role!
event: Event!
user: User!
}
This design will make render all xxWhereUniquexx unusable, which is definitely a hassle to maintain relationships. upserts will certainly be unusable for maintaining the relationships.
How would one create a relationship like this in Prisma?
For some context, each user would have a list of global roles as well, so there would already be an association between User and Role. The question concerns the "local roles" for each event.
If each user already have a value for their respective roles, there would be no need for a third table (unless there is more information you'd like to be stored in your modal layer in which case the role type should be in the Role table).
Relationships are set with the #relation directive. You can apply it to either one of two tables in a relation but for clarity I'm apply them to the Event table. The example assumes users can be part of several events.
Tables:
enum ROLE_TYPE {
TYPE_ONE
TYPE_TWO
}
User {
user_id: ID! #id
events: [Event!]!
}
Event {
event_id: ID! #id
users: [User!]! #relation(link: INLINE)
}
Role {
role_id: ID! #id
type: ROLE_TYPE
event: Event! #relation(link: INLINE)
user: User! #relation(link: INLINE)
}
You can read more about relations on the Prisma site

Given a primary key, is it possible to find all foreign keys in EF Core model?

I'm working with a rather large EF core model that has all tables and relationships defined in code that wind up attached to a context object. If I have a primary key, is there a clean/efficient way to get all the dependent columns in the model?
As an example of what I'm working with, this snippet works to find a primary key's name for a given table (the table is known by its type T which is a parameter to the function in which this code lives):
var keyName = context.Model.FindEntityType(typeof (T)).FindPrimaryKey().Properties.Select(x => x.Name).Single();
I found the answer while poking through the properties of they key object. Instead of getting the name, get the object itself, then call these two other methods to get the foreign keys that reference the primary key:
var key = context.Model.FindEntityType(typeof (T)).FindPrimaryKey().Properties.FirstOrDefault();
var foreignkeys = key.GetContainingPrimaryKey().GetReferencingForeignKeys();

Avoid Table initialize to create table in Squeryl/Activerecord

I have a database schema defined in mySQL already and I want to work on the play-2 with ActiveRecord application on top of it.
However, when I start up the project, it gives me error:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'user' already exists
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'user' already exists
and it is triggered by
org.squeryl.Schema.create(Schema.scala:181)
models.Tables$.initialize(Tables.scala:7)
This is how it looks in my Tables.scala
object Tables extends ActiveRecordTables with PlaySupport {
val users = table[User]
}
and my User.scala is:
case class User(
override val id: Long,
#Length(max=50) login: String
) extends ActiveRecord {
lazy val role = belongsTo[Role]
}
object User extends ActiveRecordCompanion[User]
I tried to skip this in my global.scala
override def onStart(app: Application) {
//Tables.initialize
}
However, it still give me the same error
Is that anyway I can bypass the create table part?
Many thanks!
Do you already have a user table in your database with a different schema?
Since you are using the PlaySupport trait, I am guessing you are using the Scala ActiveRecord Play2.1 Plugin. As noted in the wiki of that project, add the following settings in conf/play.plugins
9999:com.github.aselab.activerecord.ActiveRecordPlugin
The ActiveRecordPlugin class initializes the tables on startup. So you shouldn't have to do that from your Global onStart
Maybe you use something like MyDatabaseLibrary.create() yourself ?
Check it out. You may try to search the directory for occurences of .create
I figure it out the issue by commenting out some of the table schema in Tables.scala.
I have multiple tables declared in Table object and one of them is not existed in the database, for example:
object Tables extends ActiveRecordTables with PlaySupport {
val users = Table[User]
val role = Table[Role]
val group = Table[Group] //not exist which cause the error!
}
If one of the table is not exist in the database, the framework will create ALL tables in the order that listed in the object.
I did try to arrange the non-existing record to the top and the framework would create the table and failed to run. However, if a situation as listed above, it would give error: table 'user' is already declared and NO table would be created in database.
Would raise as a bug to Activerecord and see if there's a solution.

Entity Framework: removing entity when One-to-One PK association is specified

I have existing DB with the following structure:
I'm using EF fluent API to configure relationships between tables:
public GroupEntityConfiguration()
{
HasMany(x => x.Employees).WithRequired().HasForeignKey(x => x.GroupId).WillCascadeOnDelete(true);
}
public EmployeeEntityConfiguration()
{
HasOptional(x => x.InnerGroupMember).WithRequired();
}
With this configuration applied I can add new Employee, new InnerGroupMember or fetch data. The problem appears when I try to remove Employee. Then I get an exception:
The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
As far as I understand above exception is connected with GroupId foreign key. Trying to fix it I'm adding following line to EmployeeEntityConfiguration:
HasKey(x => new { x.Id, x.GroupId});
But after adding it I get another exception which I believe is connected with InnerGroupMember object:
Invalid column name 'Guest_Id'. Invalid column name 'Guest_GroupId'.
If I comment out InnerGroupMember navigation property and remove it's configuration, Employee can be removed.
Could you please give me a hint what I'm doing wrong and how to configure entities to be able to perform all needed operations? Thanks!
I have an existing Group entity and I want to remove Employee from the Employees Group collection:
var group = groupRepository.Find(groupId);
group.RemoveEmployee(employeeId);
_unitOfWork.Save();
RemoveEmployee function inside Group entity looks like this:
public void RemoveEmployee(int employeeId)
{
var employee = Employees.Single(n => n.Id == employeeId);
Employees.Remove(employee);
}
That's why I get an exeption:
The relationship could not be changed because one or more of the foreign-key properties is non-nullable....
After reading this post I wanted to fix it adding HasKey(x => new { x.Id, x.GroupId}); function inside EmployeeEntityConfiguration what leads to the second exception:
Invalid column name 'Guest_Id'. Invalid column name 'Guest_GroupId'.
Actually I made this step (I mean adding HasKey function) without changing DB structure. To make it work, inside Employees table I have to create composite key - combination of Id and GroupId which is also a foreign key. This modification forces changes inside InnerGroupMembers table. DB structure looks now as following:
Now I'm able to remove Employee in a way I showed at the beginning.
Anyway I'm not going for this solution. They are different ways to achieve what I want. Here are some links:
Removing entity from a Related Collection
Delete Dependent Entities When Removed From EF Collection
The relationship could not be changed because one or more of the
foreign-key properties is non-nullable
For one-to-one relationships cascading delete is not enabled by default, even not for required relationships (as it is the case for required one-to-many relationships, that is: The WillCascadeOnDelete(true) in your one-to-many mapping is redundant). You must define cascading delete for a one-to-one relationship always explicitly:
HasOptional(x => x.InnerGroupMember).WithRequired().WillCascadeOnDelete(true);
When you delete an Employee now, the database will delete the related InnerGroupMember as well and the exception should disappear.