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

derive(Insertable) into multiple tables #3535

Merged
merged 5 commits into from
Mar 9, 2023

Conversation

6293
Copy link
Contributor

@6293 6293 commented Mar 8, 2023

Closes #1314

@6293 6293 force-pushed the multiple-table-names branch from 64afc77 to cafc7f8 Compare March 8, 2023 17:34
@6293 6293 marked this pull request as ready for review March 8, 2023 17:51
@weiznich weiznich requested a review from a team March 8, 2023 20:37
match self.table_names.len() {
0 => &self.name,
1 => &self.table_names[0],
_ => abort_call_site!("Multiple table name attributes are not supported"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_ => abort_call_site!("Multiple table name attributes are not supported"),
_ => abort_call_site!("expected a single table name attribute"),

Let's avoid erroring out on the call site. Let's try doing it on the spans for table names.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ty for the insight. applied

self.table_name.as_ref().unwrap_or(&self.name)
match self.table_names.len() {
0 => &self.name,
1 => &self.table_names[0],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for just returning the first table name and not any other table name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While Insertable calls table_names method, other derives would call this to check if there is only one table name

@Ten0
Copy link
Member

Ten0 commented Mar 9, 2023

Haha I was just thinking about this today! How unlikely!

@6293 6293 force-pushed the multiple-table-names branch from cafc7f8 to 61928d0 Compare March 9, 2023 03:08
@6293 6293 requested review from pksunkara and JosueMolinaMorales and removed request for pksunkara and JosueMolinaMorales March 9, 2023 03:22
Comment on lines 1 to 5
error: expected a single table name attribute
--> tests/fail/derive/multiple_table_names.rs:29:43
|
29 | #[diesel(table_name = users, table_name = users_)]
| ^^^^^^
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be even better, if the error would be reported in this way:

Suggested change
error: expected a single table name attribute
--> tests/fail/derive/multiple_table_names.rs:29:43
|
29 | #[diesel(table_name = users, table_name = users_)]
| ^^^^^^
error: expected a single table name attribute
--> tests/fail/derive/multiple_table_names.rs:29:43
|
29 | #[diesel(table_name = users, table_name = users_)]
| ^^^^^^^^^^^^^^^^^^^ note: remove this attribute

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree. I updated the code to check the number of table_name attributes when building Model, instead of when retrieving them from Model. Now, the error will point to the table_name attribute, with a friendly note.

Btw I'm not sure how to put the note right after the highlighting carets as you provided, using proc_macro_error. Would be nice if you had any advice on this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's currently not possible, as the rust-lang proc macro crate does not provide API's for that. See rust-lang/rust#54140 for details.

@6293 6293 requested review from Mingun and removed request for pksunkara March 9, 2023 06:44
Copy link
Member

@weiznich weiznich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening this PR. This change looks fine for me 👍

@pksunkara pksunkara added this pull request to the merge queue Mar 9, 2023
Merged via the queue into diesel-rs:master with commit 25afffb Mar 9, 2023
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

Successfully merging this pull request may close these issues.

Feature : derive(Insertable) into multiple tables
6 participants