why migration is required during editing enum in prisma schema - node.js

enum OrderStatus {
paymentDone
OrderCreated
OrderConfirmed
}
iam using prima postgres db
if you add an extra literal orderCancelled in prisma schema it is asking for creating a new migration , why it behaves like this ?

This is the expected behaviour.
Initially when the enum had 3 values, this migration script would have been executed.
CREATE TYPE "OrderStatus" AS ENUM ('paymentDone', 'OrderCreated', 'OrderConfirmed');
When you are adding a new enum OrderCancelled, the OrderStatus type needs to be altered like this.
ALTER TYPE "OrderStatus" ADD VALUE 'OrderCancelled';
So Prisma Migrate initiating a new migration is the correct behavior.

Related

MikroORM: How to use 'insert_id' for subsequent persists?

How can I get the insert_id (or existing id on duplicate key error) from a persist? I'd like to use it in following persists before flush.
In my mind, even though the EntityManager doesn't have an id yet, it could still populate that and use it during flush, but I think my thinking is flawed here, I'm new to MikroORM. From what I can see in the docs, I can only achieve this using the Query Builder.
Thanks for your help!
You can just build the entity graph, the entity does not need to have a PK to be used in a relation:
const book = new Book(); // new entity, no PK
user.favorites.push(book); // doesnt matter if it has PK
await em.flush(); // if `user` was a managed entity, this will find the new book and save it to the database
console.log(book.id); // now we have the PK available on entity
In other words, the entity instance is what holds the identity, you use that, not the PK.

How can I select from a raw string in objection orm

I am trying to run a query from the results of another using with in the objection orm
ex:
Model.query().with(alias, query).select(columns).from(alias);
according to the Knex documentation which is linked from the objection docs, this should work fine. However, when I run the code, objection prepends the schema name to the alias and I get an error stating that relation schema.alias does not exist. I tried using raw but this did not help either.
ex:
Model.query().with(alias, query).select(columns).from(raw(alias));
is there a way for me to select the table/alias defined in the with method without objection prepending the schema to it?
The query method of the model I was using was overridden with code that specified the schema
ex:
class MyModel extends BaseModel {
static query() {
return super.query().withSchema(schema);
}
}
To get around this issue I used the query method of the parent class directly rather than the overridden query method of the model I was using.
This solves my current problem, but does not answer the question of whether one could omit the prepended schema name in the from method.

Why do I get "Referential integrity constraint violation" with jpaDerivedIdentifier

After using Jhipster to generate app from jdl file, I got error below at app startup. I only got this issue when I used jpaDerivedIdentifier on the OneToOne relationship to user table.
liquibase.exception.MigrationFailedException: Migration failed for change set config/liquibase/changelog/20201128181606_added_entity_constraints_RegistrationOrder.xml::20201128181606-2::jhipster:
Reason: liquibase.exception.DatabaseException: Referential integrity constraint violation: "FK_REGISTRATION_ORDER_STUDENT_ID: PUBLIC.REGISTRATION_ORDER FOREIGN KEY(STUDENT_ID) REFERENCES PUBLIC.STUDENT(ID)"; SQL statement:
ALTER TABLE PUBLIC.registration_order ADD CONSTRAINT fk_registration_order_student_id FOREIGN KEY (student_id) REFERENCES PUBLIC.student (id) [23506-200] [Failed SQL: (23506) ALTER TABLE PUBLIC.registration_order ADD CONSTRAINT fk_registration_order_student_id FOREIGN KEY (student_id) REFERENCES PUBLIC.student (id)]
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:646)
at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:53)
at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:83)
at liquibase.Liquibase.update(Liquibase.java:202)
at liquibase.Liquibase.update(Liquibase.java:179)
at liquibase.integration.spring.SpringLiquibase.performUpdate(SpringLiquibase.java:366)
at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:314)
at org.springframework.boot.autoconfigure.liquibase.DataSourceClosingSpringLiquibase.afterPropertiesSet(DataSourceClosingSpringLiquibase.java:46)
at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.initDb(AsyncSpringLiquibase.java:118)
at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.lambda$afterPropertiesSet$0(AsyncSpringLiquibase.java:93)
at io.github.jhipster.async.ExceptionHandlingAsyncTaskExecutor.lambda$createWrappedRunnable$1(ExceptionHandlingAsyncTaskExecutor.java:78)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
My JDL is:
entity Student {
birthDate LocalDate
nickName String maxlength(50)
}
entity RegistrationOrder{
paymentAmount BigDecimal required min(0)
materialFee BigDecimal required min(0)
placedDate Instant required
}
relationship OneToOne {
Student{user(login) required} to User with jpaDerivedIdentifier
}
relationship ManyToOne {
RegistrationOrder{student required} to Student
}
If I removed "with jpaDerivedIdentifier", the Jhipster app started without error. Any idea?
Probably because there are already some data loaded from CSV by Liquibase that violate the foreign key you add in your migration. Either change the data in the CSV files or change the order of migrations.
The fact that you're sharing an id with User does not help of course because the User table is created and loaded in first migration.

How to use a foreign key to point to a default value without cyclic dependency?

I have two models, service and type. A Service has many many types AND one and only default type.
Using Sequelize I'm trying to do the following
// This part is working
Service.hasMany(Type);
Type.belongsTo(Service);
// This part is not
Type.hasOne(Service, { foreignKey: 'defaultTypeId' })
The second part causes this error
Server start problem : Error: Cyclic dependency found. type is dependent of itself.
Dependency chain: device -> type -> service => type
I understand the error but I cannot find in the documentation a way to do it. Is it possible ? Is it a SQL design problem ? Do you see alternatives solutions ?
I can have a working solution by defining manually defaultTypeId in the service model, but it is no longer a foreign key, so I cannot ensure that the type ID exists.
You need to do the following
Service.hasMany(Type);
Service.hasOne(Type, { as: 'defaultType' })
Type.belongsTo(Service);

How to EF code first fluent API string primary key?

I tried to make a primary key for Member entity, I don't want to use annotation, just use fluent API:
Member{
public string MemberID {get;set;}
...
}
In MemberMapping
this.hasKey(t=>t.MemberID);
When update database I got this error:
Identity column 'MemberID' must be of data type int, bigint, smallint, tinyint, or decimal or numeric with
a scale of 0, and constrained to be nonnullable
EF supports string as PK but you have to set explicitly that property with some value when you need to create an Member's instance and save it into your DB. The only type that is identity by default in EF is int. To fix your problem I think you have two options:
Change the MemberID column in your database to not be Identity. That should solve the problem.
If you want that EF do that change for you then add this configuration:
this.hasKey(t=>t.MemberID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
Create a new migration using the Add-Migration command and try to run again Update-Database command.
Now,if your MemberID column in your DB isn't Identity and you are trying to set as Identity the MemberID PK property in your model, that also could be the cause of your issue. If that is the case, remove that configuration and try to run again Update-Database command.
From Programming Entity Framework Code First book, page 44:
In the case where the Key field is an Integer, Code First defaults to
DatabaseGeneratedOption.Identity. With a Guid, you need to explicitly
configure this. These are the only types that you can configure to be
Identity when Code First is generating the database.
the problem is not the PK but the indentity. You should have something like
property(x => x.MemberID).HasDatabaseGeneratedOption(
DatabaseGeneratedOption.None);

Resources