-
-
Notifications
You must be signed in to change notification settings - Fork 514
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
has_one
relation is not recognized with primary foreign key (class table inheritance)
#1884
Comments
My Prisma ORM schema has as 1-1 relation that sea-orm doesn't recognise too. A simplified version of my schema: -- CreateTable
CREATE TABLE "Cart" (
"id" SERIAL NOT NULL,
"userId" INTEGER NOT NULL,
CONSTRAINT "Cart_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "User" (
"id" SERIAL NOT NULL,
CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "Cart_userId_key" ON "Cart"("userId");
-- AddForeignKey
ALTER TABLE "Cart"
ADD CONSTRAINT "Cart_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; Results in: pub enum Relation {
#[sea_orm(has_many = "super::cart::Entity")]
Cart,
} While the index clearly restricts the user from having multiple carts. |
The "solution" to this problem in my case is to add unique to my primary key column that references the parent. .col(integer_uniq(Child::Id).primary_key()) This generates the following SQLite schema:
IMO the issue is simply that a primary key column is not recognized as unique by sea-orm-cli. Adding a unique constraint to a primary key should not change anything. |
Hey everyone, I just made a PR to fix this. Install cargo install sea-orm-cli --force --git https://github.com/SeaQL/sea-orm --branch cli-gen-has-one-relation |
Hi. The issue still reproduces with the setup from my description. Here's an archive just in case: Extract it, go to the root and then proceed to steps 3 and 4. |
Hey @Expurple, thanks for pointing that out! This should fix it 4bf80ce //! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0-rc.5
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "parent")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_one = "super::child::Entity")]
Child,
}
impl Related<super::child::Entity> for Entity {
fn to() -> RelationDef {
Relation::Child.def()
}
}
impl ActiveModelBehavior for ActiveModel {} |
Yes, my case is fixed now. Thank you |
Description
When I use class table inheritance, sea-orm generates a
has_many
relation from Parent to Child instead ofhas_one
. This issue is somewhat similar to #1393, but my foreign key is also a primary key and this issue happens with both PostgreSQL and SQLite. PostgreSQL is used by the actual codebase where I first encountered this, and I chose SQLite for the reproducer to make it more simple and portable.Steps to Reproduce
src/entities/parent.rs
Expected Behavior
I expected to see
has_one
instead ofhas_many
.Actual Behavior
Reproduces How Often
Always.
Workarounds
In the actual codebase with PostgreSQL and different migration code, if I turn the Child's
.primary_key()
into a.unique_key()
,has_one
is generated as expected. However, the Child entity doesn't compile afterwards because it must have a primary key. In the reproducer migration with SQLite this doesn't happen (it still generateshas_many
). I didn't investigate the reason yet. Ping me if this is important.Reproducible Example
Versions
Linux 6.2.0-33-generic
PostgreSQL 16
SQLite 3.40.1
cargo tree | grep sea-
in the actual project with PostrgeSQL:The text was updated successfully, but these errors were encountered: