Closed cpickett-ml closed 5 years ago
This is pretty interesting. In my generated code it comes out exactly as you specified. Copy pasted from generated code:
err = results.Scan(&one.ID, &one.CreatedAt, &one.UpdatedAt, &one.Name, &localJoinCol)
There's two differences between my test and yours:
CHARSET
and COLLATE
you have going on, so I used CHARSET=utf8 COLLATE=utf8_general_ci
. Could that be messing with something? I really doubt it.dev
branch of sqlboiler (though I tried on master
and v3.0.1
and still could not recreate this issue).Trying to understand how this could have happened...
I sat down this morning and ran go generate
as well as the raw sqlboiler
command and the Scan
line was generated in the correct order. I got very confused, continued messing with things, and it continued to generate Scan
in the correct order. Eventually my generate
s started putting the fields back in alphabetical order again, but I haven't been able to connect what is triggering the switch.
I'm going to continue trying for a consistent repro on my end as well.
Okay, so if I manually run the query that the sqlboiler code seems to be running to get column names against my "players" table as a test:
select
c.column_name,
c.column_type,
if(c.data_type = 'enum', c.column_type, c.data_type),
if(extra = 'auto_increment','auto_increment', c.column_default),
c.is_nullable = 'YES',
exists (
select c.column_name
from information_schema.table_constraints tc
inner join information_schema.key_column_usage kcu
on tc.constraint_name = kcu.constraint_name and tc.table_name = kcu.table_name and tc.table_schema = kcu.table_schema
where c.column_name = kcu.column_name and tc.table_name = c.table_name and
(tc.constraint_type = 'PRIMARY KEY' or tc.constraint_type = 'UNIQUE') and
(select count(*) from information_schema.key_column_usage where table_schema = kcu.table_schema and table_name = tc.table_name and constraint_name = tc.constraint_name) = 1
) as is_unique
from information_schema.columns as c
where table_name = 'players' and table_schema='sqlboil_test' and c.extra not like '%VIRTUAL%'
I (currently) get the columns back in alphabetical order. I'm not familiar enough with this part of MySQL to understand what might cause this order to change.
I was expecting this to use "show create table" to figure out the columns instead of this query, but I guess that would turn the problem into a text-parsing problem instead of just having the db provide the info in a result set for you.
Selecting ordinal_position and ordering by it causes the columns to come back in the expected order 100% of the time:
select
c.column_name,
c.column_type,
c.ordinal_position,
if(c.data_type = 'enum', c.column_type, c.data_type),
if(extra = 'auto_increment','auto_increment', c.column_default),
c.is_nullable = 'YES',
exists (
select c.column_name
from information_schema.table_constraints tc
inner join information_schema.key_column_usage kcu
on tc.constraint_name = kcu.constraint_name and tc.table_name = kcu.table_name and tc.table_schema = kcu.table_schema
where c.column_name = kcu.column_name and tc.table_name = c.table_name and
(tc.constraint_type = 'PRIMARY KEY' or tc.constraint_type = 'UNIQUE') and
(select count(*) from information_schema.key_column_usage where table_schema = kcu.table_schema and table_name = tc.table_name and constraint_name = tc.constraint_name) = 1
) as is_unique
from information_schema.columns as c
where table_name = 'players' and table_schema='sqlboil_test' and c.extra not like '%VIRTUAL%' order by `ordinal_position`
Pulled from https://dev.mysql.com/doc/refman/8.0/en/columns-table.html which states
Unlike SHOW COLUMNS, SELECT from the COLUMNS table does not have automatic ordering.
What version of SQLBoiler are you using (
sqlboiler --version
)?SQLBoiler v3.0.1
If this happened at generation time what was the full SQLBoiler command you used to generate your models? (if not applicable leave blank)
If this happened at runtime what code produced the issue? (if not applicable leave blank)
What is the output of the command above with the
-d
flag added to it? (Provided you are comfortable sharing this, it contains a blueprint of your schema)Please provide a relevant database schema so we can replicate your issue (Provided you are comfortable sharing this)
Further information. What did you do, what did you expect?
Attempting to eager load Teams when querying for Players causes an error:
This seems to be due to the generated code attempting to scan in columns in alphabetical order instead of the order they appear in the database. The generated code (app/models/players.go,
LoadTeams
in this example) contains:when it should read