Skip to content
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

Generating entity with sea-orm-cli panics when foreign key was added on table on postgres #116

Closed
ilsubyeega opened this issue Sep 23, 2023 · 3 comments

Comments

@ilsubyeega
Copy link

Description

Generating entity with sea-orm-cli panics when the foreign key is added to the table on postgres.

Steps to Reproduce

  1. Run this query to create the tables
create table "user"
(
    user_id       serial
        primary key,
    username      text                     not null
);
create table chat_channel_user
(
    user_id      integer              not null
        constraint chat_channel_user_user_user_id_fk
            references "user",
    channel_id   integer              not null,
    hidden       boolean default true not null,
    primary key (user_id, channel_id)
);
  1. run sea-orm-cli generate entity
  2. see the panics.

Expected Behavior

Should not crash and generate a schema.

Actual Behavior

It panics. I've checked with (tables without foreign keys) and it ran successfully.

Reproduces How Often

I'm guessing that this problem happens every time when foreign key is added.

Versions

  • OS: Windows 11 (x64)
  • PostgreSQL Version: PostgreSQL 15.3, compiled by Visual C++ build 1914, 64-bit
  • sea-orm-cli: 0.12.3

Additional Information

@ilsubyeega
Copy link
Author

May relavant: #28

Here's the query result of

SELECT
  "table_constraints"."constraint_schema",
  "table_constraints"."constraint_name",
  "table_constraints"."table_schema",
  "table_constraints"."table_name",
  "table_constraints"."constraint_type",
  "table_constraints"."is_deferrable",
  "table_constraints"."initially_deferred",
  "check_constraints"."check_clause",
  "key_column_usage"."column_name",
  "key_column_usage"."ordinal_position",
  "key_column_usage"."position_in_unique_constraint",
  "referential_constraints_subquery"."unique_constraint_schema",
  "referential_constraints_subquery"."unique_constraint_name",
  "referential_constraints_subquery"."match_option",
  "referential_constraints_subquery"."update_rule",
  "referential_constraints_subquery"."delete_rule",
  "referential_constraints_subquery"."table_name",
  "referential_constraints_subquery"."column_name"
FROM
  "information_schema"."table_constraints"
  LEFT JOIN "information_schema"."check_constraints" ON "table_constraints"."constraint_name" = "check_constraints"."constraint_name"
  AND "table_constraints"."constraint_catalog" = "check_constraints"."constraint_catalog"
  AND "table_constraints"."constraint_schema" = "check_constraints"."constraint_schema"
  LEFT JOIN "information_schema"."key_column_usage" ON "table_constraints"."constraint_name" = "key_column_usage"."constraint_name"
  AND "table_constraints"."constraint_catalog" = "key_column_usage"."constraint_catalog"
  AND "table_constraints"."constraint_schema" = "key_column_usage"."constraint_schema"
  AND "table_constraints"."table_catalog" = "key_column_usage"."table_catalog"
  AND "table_constraints"."table_schema" = "key_column_usage"."table_schema"
  AND "table_constraints"."table_name" = "key_column_usage"."table_name"
  LEFT JOIN (
    SELECT
      DISTINCT "referential_constraints"."constraint_name",
      "referential_constraints"."unique_constraint_schema",
      "referential_constraints"."unique_constraint_name",
      "referential_constraints"."match_option",
      "referential_constraints"."update_rule",
      "referential_constraints"."delete_rule",
      "constraint_column_usage"."table_name",
      "constraint_column_usage"."column_name"
    FROM
      "information_schema"."referential_constraints"
      LEFT JOIN "information_schema"."constraint_column_usage" ON "referential_constraints"."constraint_name" = "constraint_column_usage"."constraint_name"
  ) AS "referential_constraints_subquery" ON "table_constraints"."constraint_name" = "referential_constraints_subquery"."constraint_name"
WHERE
  "table_constraints"."table_schema" = 'public'
  AND "table_constraints"."table_name" = 'chat_channel_user'
  AND "referential_constraints_subquery"."table_name" NOT IN (
    SELECT
      "pg_class"."relname"
    FROM
      "pg_inherits"
      JOIN "pg_class" ON "pg_inherits"."inhrelid" = "pg_class"."oid"
      AND "pg_class"."relkind" IN ('r', 't', 'v', 'm', 'f', 'p')
  )
ORDER BY
  "table_constraints"."constraint_name" ASC,
  "key_column_usage"."ordinal_position" ASC,
  "referential_constraints_subquery"."unique_constraint_name" ASC,
  "referential_constraints_subquery"."constraint_name" ASC
constraint_schema constraint_name table_schema table_name constraint_type is_deferrable initially_deferred check_clause column_name ordinal_position position_in_unique_constraint unique_constraint_schema unique_constraint_name match_option update_rule delete_rule table_name column_name
public 2200_18205_1_not_null public chat_channel_user CHECK NO NO user_id IS NOT NULL null null null null null null null null null null
public 2200_18205_2_not_null public chat_channel_user CHECK NO NO channel_id IS NOT NULL null null null null null null null null null null
public 2200_18205_3_not_null public chat_channel_user CHECK NO NO hidden IS NOT NULL null null null null null null null null null null
public chat_channel_user_pkey public chat_channel_user PRIMARY KEY NO NO null user_id 1 null null null null null null null null
public chat_channel_user_pkey public chat_channel_user PRIMARY KEY NO NO null channel_id 2 null null null null null null null null
public chat_channel_user_user_user_id_fk public chat_channel_user FOREIGN KEY NO NO null user_id 1 1 public user_pkey NONE NO ACTION NO ACTION user user_id_i

However, the values are different than expected.

result.constraint_schema: "public"
result.constraint_name: "chat_channel_user_user_user_id_fk"
result.table_schema: "public"
result.table_name: "chat_channel_user"
result.constraint_type: "FOREIGN KEY"
result.is_deferrable: "NO"
result.initially_deferred: "NO"
result.check_clause: None
result.column_name: Some("user_id")
result.ordinal_position: Some(1)
result.position_in_unique_constraint: Some(1)
result.unique_constraint_schema: Some("public")
result.unique_constraint_name: Some("user_pkey")
result.match_option: Some("NONE")
result.update_rule: Some("NO ACTION")
result.delete_rule: Some("NO ACTION")
result.referential_key_table_name: None
result.referential_key_column_name: None

@ilsubyeega
Copy link
Author

I've found that the user that connected does not have permission for something. I got fixed by adding permission to the user.
I'm not going to close this issue for better handling of this or so.

@tyt2y3
Copy link
Member

tyt2y3 commented Sep 26, 2023

This seems to be a very rare situation, the column shouldn't return null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants