Show HN: Sqlkit – Golang SQL package with nested transactions
github.com
github.com
[0] https://upper.io/db.v3No frills, just a little reflection to tie struct fields <=> SQL params.
This just seems...wrong. Why would the column be nullable if you don’t actually care if it’s NULL?
if I read out a nullable text field and write the same struct back into the database is it going to insert an empty string into that column?
Also related, how would auto incr pk work?
You should use UUIDs only when, you know, you need a UUID.
Edit: getting a lot of heat for this comment from people misunderstanding it. I’m not saying “null” shouldn’t exist, just that it shouldn’t be the default. Side point: most of the time people think they need null they don’t.
NULL is what we've got though. setting columns to not null is leaps ahead of most programming languages anyway.
This is far from clear
However if you’re main body of experience is Perl (for example) then NULL isn’t ever an issue.
Eg Perl doesn’t have a nil type like Go, so variables passed to DBI which are NULL in the returned SQL query will be undef’ed instead. Effectively a bit like NULL but it only causes a problem if your running with strict flags (which you shouldn’t do on production systems anyway for various reasons; but that’s another topic entirely) otherwise that value falls back to the language default of “”/0/false.
With languages like Go you can easily end up with a panic if you’re not careful (where you’re trying to cast a nil interface{}). Which, I’m guessing, is why this Go library being discussed here does it’s own NULL checking.
How is this any different to the runtime error you’d get in Perl if you attempted a method call on an undefined value you’d expected to be an object?
So I’m not going to argue that Perl doesn’t also have its own hidden traps (a great many of them in fact) nor that it’s interpretation of OOP isn’t terrible but the scope of this discussion was about nullable types and not anything else you now want to draw into the argument.
That would be Perl 5. Perl 6 does. From https://docs.perl6.org/type/Nil :
"The value Nil may be used to fill a spot where a value would normally go, and in so doing, explicitly indicate that no value is present."
However if we’re talking from idealistic perspective then you’d have your own DBAs who would create the DB and write the SQL so your developers just call your (for example) stored procedures like APIs rather than writing their own - often unoptimised - SQL themselves.
Nullable is different than empty. Consider a contrived example where I’ve extended a user table to include a new column “fullName”. By making this a nullable column, I can determine that a user has not set this value. But maybe the user sets it to an empty string. The API might want to know this and handle it differently — prompting for a value if it’s null, but ignoring “” as “this is what the user wanted to set”.
Without nullable values, I would have to create bit values in a lot of tables indicating if the value was ever set.
Another example is time stamps. Databases have timestamps for createdAt and updatedAt often. There are people who set updatedAt when they set createdAt, but there are people who don’t. I’m in the later camp. If an object was created but never updated, I want to be able to know that. How would I do this if the updatedAt column is not nullable? I’d either have to set a bit field that it has been updated, which feels suboptimal, or I could set it to something like “time.Empty{}”, which is a magic number tied to the programming language? There are plenty of other solutions here, sure, but what’s wrong with nullable fields?
Re your timestamp example, just compare if created is the same as updated. That’s a really easy problem to solve. Though I have seen other scenarios when a timestamp needed to by nullable so I agree there will be edge cases.
I agree that null does have its uses (it’s basically essential for outer joins) but most of the time people think they need null they don’t actually need it. Plus it frequently it causes hidden faults, so it’s usually best to avoid allowing nullable fields unless you’re sure there isn’t any other easy way.
You may not have this use case you in your application, but I do. I have real scenarios that need to know if a value is initialized or not. I agree that I could solve this in other ways, but nullable columns just aren’t an archaic database option just because you can implement the same behavior in the application code or by rewriting the SQL query.
I have ran into some instances when I have needed nullable fields in relational databases as well. I’m not claiming null shouldn’t exist. My point was that they should be an opt in rather than opt out because they cause more problems than generally they solve.
Though “deleted” isn’t really the right term here either because you’re not really deleting data, just removing it from view.
Secondly, I’d already given outer joins as an example of why SQL needs to support NULL.
I would be nice that people like yourself bothered to read the comments your reacting to instead of jumping off the deep end (-4 on my original post now and people like yourself are literally just reiterating the same points I’ve been making. It’s insane)
It’s also worth remembering that five people is still only a very small subset of the hundreds that would have read the comment and did understand/agree but didn’t bother to comment for fear of the wrath of those who have already expressed disagreement. For example I was actually voted up several times before the others commented (as well as a few times since).
So it’s really not worth blaming individuals for the comments of others. The only possible outcome from that is starting another, much stupider, argument.
That's not really relevant to this discussion, though -- once you do have a nullable column in your schema, it doesn't make sense in the Go layer to pretend you don't.
It’s tangential but still relevant to the discussion.
> once you do have a nullable column in your schema, it doesn't make sense in the Go layer to pretend you don't.
I agree and never suggested what this Go API was doing did make sense.
Assuming that the nullable foreign key problem could be solved, I think that NULL is valuable enough (and not that rare) that it ought to be the database owner's choice whether to allow them or not, as opposed to the superuser (not sure which way you're suggesting).
Not really. Defaults change all the time. I’m not asking for a feature to be removed after all.
I don’t deny the change in any specific RDBMS would have to be carefully considered but I’ve worked on a lot harder projects to make transitions in default behaviours than this would be so it’s certainly doable if the momentum was there. However I doubt enough people care enough (if this thread is anything to go by).
> Assuming that the nullable foreign key problem could be solved
Offhand I can think of a few ways to solve that but I also think it’s one of the instances where NULL makes the most sense
> I think that NULL is valuable enough (and not that rare) that it ought to be the database owner's choice whether to allow them or not, as opposed to the superuser (not sure which way you're suggesting).
I completely agree the DBA should still have the power to enable it. My point was just that columns shouldn’t default to being nullable during a CREATE.
To be honest, this could just as easily be “fixed” in graphical database management tools. That would at least enable seasoned DBAs to utilise NULL while preventing others from some of the pitfalls that nullable fields can introduce in some languages. However I still stand by my point that it’s not really a sane default for the way most people architect databases these days (or even “should” be architecting).
The initial thought behind it: Go programmers generally use values for scalars instead of pointers. Can we make an sql mapper that reflects that general practice?
Maybe the answer is no.
I spend something like 10% of my working hours un-fucking data that was poisoned by lazy, inconsistent, and/or incorrect handling of missing data.
This is what makes Excel such a dumpster fire for handling data. It is absolutely the wrong decision to make any new piece of software that handles missing data like this.
- the link to GoDoc is not working yet: https://godoc.org/github.com/colinjfw/sqlkit
- Typo in the README: Unmarsal instead of Unmarshal
Without going through everything in detail, why would I pick this over the very popular sqlx? https://github.com/jmoiron/sqlx
Short answer: It's sqlx + a query builder + some extras around transaction management.