-
Hey everyone, I'm a beginner here working with Vue and Pinia-ORM, and I have the following problem that I can't solve on my own. I currently have two models in an app: clients and projects. I call them using a fetch request and receive the response with no problem. I can also save them in their repos and everything fires perfectly, except for when I try to define the relationship between the two. When i do i get the following error message:
I adapted them from SQLAlchemy models, here they are:
Client Model:
Pinia-ORM Models
Clients:
What am I missing here? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
At a glance, it looks like you may need a @BelongsTo(() => clients, 'client_id') declare client_id: clients to this: @BelongsTo(() => Client, 'client_id') declare client: client
import { Model } from "pinia-orm";
import { Attr, BelongsTo, Bool, Num, Str, Uid } from 'pinia-orm/dist/decorators'
import Client from '@app/stores/pinia-orm/models/ClientModel'
export default class ProjectModel extends Model {
static entity = 'projects'
static primaryKey = 'project_id'
@Uid() declare project_id: number
@BelongsTo(() => Client, 'client_id') declare client: Client | null
// the below line is new. We must also declare the client_id.
@Attr() declare client_id: string
@Str(null, { notNullable: true }) declare name: string
@Str(null, { notNullable: true }) declare description: string | null
@Bool(true) declare active: boolean
@Attr() declare start_date: Date | null
@Attr() declare end_date: Date | null
@Num(ProjectModelHelpers.serverDefaultValues.statusIdDefaultValue, { notNullable: true })
declare status_id: number
@Num(ProjectModelHelpers.serverDefaultValues.typeIdDefaultValue, { notNullable: true })
declare type_id: number
@Num(ProjectModelHelpers.serverDefaultValues.budgetDefaultValue, { notNullable: true })
declare budget: number
}
import { Model } from "pinia-orm";
import { Bool, HasMany, Str, Uid } from 'pinia-orm/dist/decorators'
import Project from '@app/stores/pinia-orm/models/ProjectModel'
export default class ClientModel extends Model {
static entity = 'clients'
static primaryKey = 'client_id'
@Uid() declare client_id: number
@Str(null, { notNullable: true }) declare name: string
@Bool(null, { notNullable: true }) declare active: boolean
@HasMany(() => projects, 'client_id') declare projects: Project[]
} Also hot tip! To add syntax highlighting to your code in github, follow your first three bacticks with the language. |
Beta Was this translation helpful? Give feedback.
At a glance, it looks like you may need a
client_id
on the "Projects" model. I'm going to rewrite some of this code, and change the naming to make it more inline with what you'd usually see with PiniaORM. Specifically, changing this line:to this:
ProjectModel.ts
(note, this is usually pascal, singular. You may also want to drop the wordModel
so its readsProject.ts
)