-
-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot persist an entity with a 1-to-1 reference to another entity #140
Comments
Additional info below: Profile Schema import { BigIntType, EntitySchema } from '@mikro-orm/core';
import { Profile } from './profile.entity';
import { User } from './user.entity';
export const profileSchema = new EntitySchema<Profile>({
class: Profile,
tableName: 'profile',
forceConstructor: true,
properties: {
id: {
type: BigIntType,
primary: true,
autoincrement: true,
},
user: {
entity: () => User,
reference: '1:1',
mappedBy: 'profile',
ref: true,
},
imageUrl: {
type: String,
},
},
}); User Schema import { BigIntType, EntitySchema } from '@mikro-orm/core';
import { User } from './user.entity';
import { Address } from './address.entity';
import { Profile } from './profile.entity';
export const userSchema = new EntitySchema<User>({
class: User,
tableName: 'user',
forceConstructor: true,
properties: {
id: {
type: BigIntType,
primary: true,
autoincrement: true,
},
firstName: {
type: 'string',
},
lastName: {
type: 'string',
},
email: {
type: 'string',
},
addresses: {
reference: '1:m',
entity: () => Address,
mappedBy: 'user',
},
profile: {
reference: '1:1',
entity: () => Profile,
inversedBy: 'user',
nullable: true,
},
createdAt: {
type: 'timestamp',
},
updatedAt: {
type: 'timestamp',
},
},
}); |
because it is inverse side, as such it does not represent any database column. your schema is not in sync with entity definition apparently, if you expect such column to exist. swap |
Hey Martin, thanks for the speedy reply! But can we define
Btw I thought the |
No you can't. I told you to swap those, so where you have
Yes, and you had it on the inverse instead. The owning side is where you have the database column. FYI the |
Got it! Thanks, Martin! |
Describe the bug
User
entity class with its schemaProfile
entity class that has a one-to-one relationship with the UserProfile
entity, passing theuser
as an attributeNotNullConstraintViolationException: insert into "profile" ("image_url") values ('https://example.com/image.png') returning "id" - null value in column "user_id" violates not-null constraint
Stack trace
To Reproduce
Steps to reproduce the behavior:
yarn
docker compose up -d
yarn test
Expected behavior
The Profile should be saved correctly
Additional context
Summary of the test case that is failing:
Versions
The text was updated successfully, but these errors were encountered: