How would I define an endDate to be 12 months in the future in my schema.prisma. For example:
model Subscription {
id Int #id #default(autoincrement())
startDate DateTime #default(now())
endDate DateTime #default( How to define 1 year in the future?)
...
}
You would need to create a custom function that returns the time after one year and call it via the dbgenerated function.
Example: The function created in the database is called get_end_time. You can define it in schema as
model Subscription {
id Int #id #default(autoincrement())
startDate DateTime #default(now())
endDate DateTime #default(dbgenerated("get_end_time()"))
}
Related
I have to get total price in month from a date yy-mm-dd with jpql query but I can't do it.
#Query(value = "select new com.talan.food.dto.MonthIncomes( function('date_format',date,'%Y-%m'),SUM(p.price)) from Reservation p group by function('date_format',p.date,'%Y-%m')" )
public List<MonthIncomes> getIncomeByMonth();
And in the table of entity I have:
public class Reservation {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private double price;
#ManyToOne #JoinColumn(name="userId" )
public User user;
private LocalDate date;
private boolean confirmed;
}
And I will put the result in the class :
public class MonthIncomes {
private LocalDate date ;
private double price;
public MonthIncomes (LocalDate date, double price) {
this. date= date;
this.price = price;
}
}
You could something like this in your query
SELECT p.date as date ,
SUM(p.price) as price
FROM reservation p
WHERE p.date >= '2022-01-01'
GROUP BY
EXTRACT(month from p.date)
depending on the amount of yours you have to go back you would have to change the extract statement a bit. Nevertheless your problem looks like something that you could approach with the extract function
https://www.postgresqltutorial.com/postgresql-date-functions/postgresql-extract/
i resolve the problem by using this syntaxe:
#Query(value = "select new com.food.dto.MonthIncomes(EXTRACT(month from p.date),SUM(p.price)) from Reservation p group by EXTRACT(month from p.date) ORDER BY EXTRACT(month from p.date)")
public List<MonthIncomes> getIncomeByMonth();
I want to create this database deisng https://i.stack.imgur.com/2QYip.png
I need to make model of Station and TrackSegment (A track segment ties two stations together.)
This is my prisma schema but it give me this error "Error validating model 'station': the unique index definitions refers to the field code multiple times.
model Station {
id Int #id #default(autoincrement())
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
name String
code String #unique
}
model TrackSegment {
id Int #id #default(autoincrement())
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
sourceId String
destinationId String
stationCode Station #relation(fields: [destinationId, sourceId], references: [code, code])
}
I imagine this is how the data will look.
Station: [
{
name: 'kalisetail'
code: 'KLS
},
{
name: 'rogojampi'
code: 'RGJ
}]
then the TrackSegment is
id: 1
sourceId: KLS
destinationId: RGJ
both sourceId and destinationId refer to the same field(code) of model Station
You will need to add a second Station relation on your TrackSegment field to differentiate between the source the destination.
model Station {
id Int #id #default(autoincrement())
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
name String
code String #unique
sourceSegments TrackSegment[] #relation("source")
destinationSegments TrackSegment[] #relation("destination")
}
model TrackSegment {
id Int #id #default(autoincrement())
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
sourceId String
destinationId String
sourceStationCode Station #relation(name: "source", fields: [sourceId], references: [code])
destinationStationCode Station #relation(name: "destination", fields: [destinationId], references: [code])
}
Notice I have always disambiguated the relations by giving them different names.
I am writing a postgreSQL database. The ID must be auto-incrementing.
model User {
id String #id #default(cuid())
name String?
email String? #unique
emailVerified DateTime? #map("email_verified")
image String?
createdAt DateTime #default(now()) #map(name: "created_at")
updatedAt DateTime #updatedAt #map(name: "updated_at")
posts Post[]
accounts Account[]
sessions Session[]
##map(name: "users")
}
Result
If you write the ID manually, you can enter any value as in the first entry, but autocomplete generates the code as in the second entry. I got the code from the site Vercel.
You need to use autoincrement function.
Here's how it would look in User model:
model User {
id Int #id #default(autoincrement())
name String?
email String? #unique
emailVerified DateTime? #map("email_verified")
image String?
createdAt DateTime #default(now()) #map(name: "created_at")
updatedAt DateTime #updatedAt #map(name: "updated_at")
posts Post[]
accounts Account[]
sessions Session[]
##map(name: "users")
}
I can in Prisma create columns without data, without time?
my model at the moment:
model modelName {
id Int #id #default(autoincrement())
createdAt DateTime #default(now())
lastNumber Int
}
Actual results:
2 2022-04-19 12:28:04.591+00 45
I want to generate records like this:
2 2022-04-19 45
To store only the date portion you could use the native #db.date attribute.
That would just store the date and not store the time.
In your schema file you could update the model as below to just store the date portion:
model modelName {
id Int #id #default(autoincrement())
createdAt DateTime #default(now()) #db.Date
lastNumber Int
}
Here's a reference to Prisma's PostgreSQL DateTime attribute: Reference
I need some help to understqnd how to set my data modelisation.
What i'm trying to do is :
Reference a group ID in User model. A user can only be in one group, this is what i've done below.
Reference in Group Model all users that are inside a group. This is what I need help with.
model User {
id String #id #default(uuid())
email String #unique
role Role #default(USER)
group Group? #relation(fields: [group_id], references: [id], onDelete: SetNull)
group_id String
created_at DateTime #default(now())
updated_at DateTime #updatedAt
##map("user")
}
model Group {
id String #id #default(uuid())
name String
users User[]
created_at DateTime #default(now())
updated_at DateTime #updatedAt
deleted Boolean #default(false)
##map("group")
}
How can I make this list of User when I already have users User[] for the relation in User model ?
(BTW i've read the doc but i'm totally lost...)
I think your code is correct, you can use prisma-client to test
This is an example of this relation
model Agents {
id Int #id #default(autoincrement()) #db.UnsignedInt
name String? #db.VarChar(255)
owner String? #db.VarChar(255)
address String? #db.Text
lat String? #db.VarChar(10)
lng String? #db.VarChar(10)
tel String? #db.VarChar(13)
mobile String? #db.VarChar(11)
workTime String? #db.VarChar(255)
province Provinces #relation(fields: [provinceId], references: [id])
provinceId Int #db.UnsignedInt
createDateTime DateTime #default(now()) #db.Timestamp(0)
updateDateTime DateTime? #db.Timestamp(0)
##index([provinceId], name: "provinceId")
##map(name: "agents")
}
model Provinces {
id Int #id #default(autoincrement()) #db.UnsignedInt
name String? #db.VarChar(255)
coords String? #db.Text
createDateTime DateTime #default(now()) #db.Timestamp(0)
updateDateTime DateTime? #db.Timestamp(0)
agents Agents[]
##map(name: "provinces")
}