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

[sea-orm-cli] Unique key handling #191

Closed
cemoktra opened this issue Sep 23, 2021 · 13 comments
Closed

[sea-orm-cli] Unique key handling #191

cemoktra opened this issue Sep 23, 2021 · 13 comments
Assignees

Comments

@cemoktra
Copy link
Contributor

Even with verbose mode i'm not sure why this sea-orm-cli generate entity fails:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ColumnDecode { index: "2", source: UnexpectedNullError }', /home/bastian/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-core-0.5.7/src/row.rs:73:37
stack backtrace:
   0: rust_begin_unwind
             at /rustc/c8dfcfe046a7680554bf4eb612bad840e7631c4b/library/std/src/panicking.rs:515:5
   1: core::panicking::panic_fmt
             at /rustc/c8dfcfe046a7680554bf4eb612bad840e7631c4b/library/core/src/panicking.rs:92:14
   2: core::result::unwrap_failed
             at /rustc/c8dfcfe046a7680554bf4eb612bad840e7631c4b/library/core/src/result.rs:1599:5
   3: <sea_schema::mysql::query::constraint::ConstraintQueryResult as core::convert::From<&sqlx_core::mysql::row::MySqlRow>>::from
   4: <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::fold
   5: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter
   6: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
   7: futures_util::stream::stream::StreamExt::poll_next_unpin
   8: <futures_util::stream::futures_ordered::FuturesOrdered<Fut> as futures_core::stream::Stream>::poll_next
   9: <futures_util::stream::stream::collect::Collect<St,C> as core::future::future::Future>::poll
  10: <futures_util::future::join_all::JoinAll<F> as core::future::future::Future>::poll
  11: sea_orm_cli::run_generate_command::{{closure}}
  12: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
  13: std::thread::local::LocalKey<T>::with
  14: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
  15: async_io::driver::block_on
  16: std::thread::local::LocalKey<T>::with
  17: async_std::task::builder::Builder::blocking
  18: sea_orm_cli::main

I know that i had problems with this database also with diesel but the error and the verbose log are not very helpful in this case.

@tyt2y3
Copy link
Member

tyt2y3 commented Sep 23, 2021

https://github.com/SeaQL/sea-schema/blob/b5063a9fa3954c12a22b6a15716e73946b519213/src/mysql/query/constraint.rs#L34-L41

constraint_name
column_name
referenced_table_name
referenced_column_name
update_rule
delete_rule

Which of these column is null in your information_schema.key_column_usage?

If you know which table caused it to panic, we can query the information_schema ourselves and see.

@cemoktra
Copy link
Contributor Author

cemoktra commented Sep 23, 2021

SELECT * FROM information_schema.key_column_usage
WHERE 
table_schema="*********"
AND(
constraint_name IS NULL
OR column_name IS NULL 
OR referenced_table_name IS NULL 
OR referenced_column_name IS NULL
) GROUP BY table_name;

193 rows in set (0.00 sec)

and if i reduce it to be not PRIMARY there are still 57 rows remaining where referenced_table_name and referenced_column_name are null while not having update_rule and delete_rule in the schema

@cemoktra
Copy link
Contributor Author

Seems like all of those are UNIQUE_KEYs

@tyt2y3
Copy link
Member

tyt2y3 commented Sep 23, 2021

Well, nice info.
I will see whether I can reproduce and fix that.
It will be helpful if you have a minimal schema .sql to share.

@cemoktra
Copy link
Contributor Author

Seems to be a grown problem of the existing database, i cannot reproduce it with a minimal sample at the moment

@tyt2y3
Copy link
Member

tyt2y3 commented Sep 23, 2021

Oh then may be the only thing we can do is to modify the query such that these columns will not be NULL.

@cemoktra
Copy link
Contributor Author

cemoktra commented Sep 23, 2021

Ok i got it:

USE sea_error;

CREATE TABLE `table_2` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `table_1` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `table_2_id` bigint(20) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `table_2_id` (`table_2_id`),
  CONSTRAINT `table_2_id` FOREIGN KEY (`table_2_id`) REFERENCES `table_2` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

@tyt2y3
Copy link
Member

tyt2y3 commented Sep 23, 2021

Cool! That is really helpful.

@tyt2y3
Copy link
Member

tyt2y3 commented Sep 24, 2021

The crash has been fixed, at least for the schema in question.
You can try installing the unreleased version of sea-orm-cli 0.2.4 by

cargo install sea-orm-cli --git https://github.com/SeaQL/sea-orm

There is still an inaccurancy (the unique annotation is missing), but hopefully it will not block your workflow for now )

@cemoktra
Copy link
Contributor Author

I will try it later and give feedback. Thanks

@billy1624
Copy link
Member

There is still an inaccurancy (the unique annotation is missing), but hopefully it will not block your workflow for now )

Added on 360436c. For expanded format, it already annotated .unique().

@billy1624 billy1624 self-assigned this Sep 24, 2021
@cemoktra
Copy link
Contributor Author

Works as expected. Thanks

@tyt2y3 tyt2y3 changed the title Better error handling or logging [sea-orm-cli] Unique key handling Sep 25, 2021
@tyt2y3
Copy link
Member

tyt2y3 commented Sep 26, 2021

Released in sea-orm-cli 0.2.4 on crates.io

@tyt2y3 tyt2y3 closed this as completed Sep 26, 2021
arpancodes pushed a commit to arpancodes/sea-orm that referenced this issue Apr 8, 2022
`driver/postgres` handle non-exhaustive `Value`
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

3 participants