[OT] Idea to get all columns from multiple tables in DB

Steven Schveighoffer schveiguy at gmail.com
Wed Jan 8 23:03:29 UTC 2020

On 1/8/20 2:19 AM, Jacob Carlborg wrote:
> On 2020-01-07 22:01, Steven Schveighoffer wrote:
>> I have a database serialization package that I use to fetch data from 
>> a database. The column names are all identified or tagged 
>> appropriately, so I just use:
>> connection.query("SELECT * FROM user").byItem!User;
>> and it serializes the data from the resulting rows into the struct 
>> User type for consumption as a range.
>> However, if there's a join, it poses a problem. Let's say I have an 
>> Equipment item, which is owned by a user. I can select both the 
>> equipment and the owner via:
>> SELECT * FROM equipment left join user on (user.id = equipment.owner_id)
>> However, this causes problems because I get duplicate columns (e.g. 
>> name might be in both, or id). Which one goes with which?
> IIRC Rails is using the fully qualified column name for everything. 
> Instead of using "*" it enumerates all columns of, in your case, User. 
> It will inspect User to figure out all the column names. You can easily 
> do the same thing in D by inspecting the fields on User.

Yeah, this is generally the case when generating SQL from types. I like 
to write queries to make them the most efficient I can, without having 
to deal with quirks between ORM requirements and the data in the table 
(I'm dealing with an existing data layout, which does not lend itself 
always to ORM usage).


More information about the Digitalmars-d mailing list