[Backend #4] Generate CRUD Golang code from SQL | Compare db/sql, gorm, sqlx & sqlc

  Рет қаралды 77,411

TECH SCHOOL

TECH SCHOOL

Күн бұрын

Пікірлер: 179
@TECHSCHOOLGURU
@TECHSCHOOLGURU 3 жыл бұрын
Hello guys, if you're using Windows, you can use sqlc inside a Docker container: docs.sqlc.dev/en/latest/overview/install.html#docker And generate code using this command: docker run --rm -v $(pwd):/src -w /src kjconroy/sqlc generate I think you will need to replace the $(pwd) with your current folder because it's kind of volume mapping.
@mustafaemin7389
@mustafaemin7389 3 жыл бұрын
Open powershell in your project directory then paste that: docker run --rm -v ${pwd}:/src -w /src kjconroy/sqlc generate That worked on my system.
@cariyaputta
@cariyaputta 2 жыл бұрын
I'm on Windows 11 and facing another issues, any idea on this: project-folder> docker run --rm -v :/src -w /src kjconroy/sqlc generate error parsing sqlc.json: file does not exist make: *** [Makefile:17: sqlc] Error 1
@cariyaputta
@cariyaputta 2 жыл бұрын
I have resolved it. The issue is that when run it inside make, it will ignore the ${pwd} part If some one run into this issue with the Makefile. add these lines into your file: makeFileDir := $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) sqlc: docker run --rm -v $(makeFileDir):/src -w /src kjconroy/sqlc generate
@mfc1190
@mfc1190 2 жыл бұрын
@@cariyaputta Thanks, this was helpful!
@thorfinnthegoatmc
@thorfinnthegoatmc 2 жыл бұрын
@@mustafaemin7389 Thank you so much bro
@jarriabidi1438
@jarriabidi1438 4 жыл бұрын
this is gold. i have been very confused about how to add type safety to my db code while maintaining performance and not learning a ton of new syntax. this sqlc thing is amazing
@TECHSCHOOLGURU
@TECHSCHOOLGURU 4 жыл бұрын
yes, it's really is amazing :)
@Dzikrullah_999
@Dzikrullah_999 Жыл бұрын
you are a talented and patient teacher. The world is better with you Tech School !
@CiroSilvano-yn7ow
@CiroSilvano-yn7ow Жыл бұрын
Thank you for this amazing content! I'd like to add that the pq driver is less maintained and hence may not be the best choice anymore. After following the lecture I migrated to pgx version 4, which is now well supported by sqlc (v5 is still experimental).
@vonmutinda4888
@vonmutinda4888 4 жыл бұрын
Even with months of writing Go code, I still find these videos super useful. Would you kindly touch on Design Patterns in later videos. Thanks.
@TECHSCHOOLGURU
@TECHSCHOOLGURU 4 жыл бұрын
Thanks von. Great suggestion, I will try to make some videos about it in the future.
@berkekaancetinkaya8721
@berkekaancetinkaya8721 3 жыл бұрын
Following this with a windows machine is a nightmare :) Besides the series is awesome!!
@vedantshinde2277
@vedantshinde2277 3 жыл бұрын
Did you manage to get it running? I'm getting an error while executing this command: "docker run --rm -v C:/Users/vedant/src -w /src kjconroy/sqlc generate" It either says that "error parsing sqlc.json: file does not exist" or schema.sql doesn't exist.
@berkekaancetinkaya8721
@berkekaancetinkaya8721 3 жыл бұрын
@@vedantshinde2277 No i have found an another solution which is switching to Linux 😂
@MrVirusbkhn
@MrVirusbkhn 4 жыл бұрын
i found new horizon, all of your tutorial which exactly is my orientation. Thank for your effort, hope you will product quality videos, like this :D
@TECHSCHOOLGURU
@TECHSCHOOLGURU 4 жыл бұрын
Glad I could help!
@sekirandahamza1260
@sekirandahamza1260 3 жыл бұрын
I feel sad that such an informative video (series) has over 13989 views and just few likes. Like the damn video, it's the only payment you're going to make for this free awesome information. Thanks TECH SCHOOL ,keep it up.
@TECHSCHOOLGURU
@TECHSCHOOLGURU 3 жыл бұрын
Thanks a lot, Sekiranda!
@AdityaFingerstyle
@AdityaFingerstyle 3 жыл бұрын
18:16, rows.Close() is already deferred and then called again.
@TECHSCHOOLGURU
@TECHSCHOOLGURU 3 жыл бұрын
nice catch :D
@GmanGavin1
@GmanGavin1 Жыл бұрын
Wow, I'm very surprised by how informative this video is.
@TECHSCHOOLGURU
@TECHSCHOOLGURU Жыл бұрын
Glad you think so!
@dwiwahyudi9698
@dwiwahyudi9698 Жыл бұрын
Hi, Thanks for the video, this really help me a lot. Btw, For parameters (CreateAccountParams, etc...), Are there any reason not used parameters in models.go (Account, Entry, etc..) like Account models object in return, ex: > func (q * Queries) CreateAccount(ctx context.Context, arg Account) (Account, error) Because, I already try to use Account models as input and return for CreateAccount function, and the result is still the the same. Thanks.
@budipr6448
@budipr6448 2 жыл бұрын
Hi, I was trying it and got this message : the PostgreSQL engine does not support Windows. Any advice would be appreciated, thanks
@Saulsotog
@Saulsotog 3 жыл бұрын
this series of videos are really great
@TECHSCHOOLGURU
@TECHSCHOOLGURU 3 жыл бұрын
Thanks, Saul Soto!
@hoonj2500
@hoonj2500 Жыл бұрын
Nice utilization of Makefile
@shellixupdates6125
@shellixupdates6125 2 жыл бұрын
If you should not edit the generated files manually, then how do you use `json:",omitempty"` with sqlc ?
@orkhanrustamli2039
@orkhanrustamli2039 2 жыл бұрын
Hey, great tutorial as always. Question, in my work usually db comes with pre defined schema which is defined by db engineers which means I do no have schema at hand to feed sqlc. What would be your approach in this case?
@TECHSCHOOLGURU
@TECHSCHOOLGURU 2 жыл бұрын
Hey Orkhan, I think sqlc automatically queries the database to know its schema. You can give it a try and please let me know if it's not the case.
@orkhanrustamli2039
@orkhanrustamli2039 2 жыл бұрын
@@TECHSCHOOLGURU I could not find property to show database address so that sqlc will try to fetch schema from db. I think this is the only major downside of sqlc,
@user-sk4nt5bm5r
@user-sk4nt5bm5r Жыл бұрын
Could you update this to reflect sqlc version 2 config file? It is quite different. Thanks for the course btw. I think v2 sqlc.yaml should look like this: version: "2" sql: - schema: "./db/migration/" queries: "./db/query/" engine: "postgresql" gen: go: package: "db" emit_json_tags: true out: "./db/sqlc"
@AhmedAli-jx9ie
@AhmedAli-jx9ie Жыл бұрын
up
@aliadel1723
@aliadel1723 Жыл бұрын
Thank you bro
@apoorvamishra5952
@apoorvamishra5952 Жыл бұрын
slqc.yaml is different in the latest version 1.17.2 and with lesser options
@rishabhagrawalzra
@rishabhagrawalzra 2 жыл бұрын
Hi @TECH SCHOOL, what is the meaning of context.Context, and why we are passing this. Can you pls elaborate on this? Thanks loved your content
@TECHSCHOOLGURU
@TECHSCHOOLGURU 2 жыл бұрын
We use context to control the lifetime of a process, so we can easily stop processing if the context has been canceled. For example, if the client calls your API and only waits for 1 second, but your API takes 5 seconds to complete => then after 1 second, the client has already canceled the request => so the server can stop working on it immediately.
@MorbidPasta
@MorbidPasta 2 жыл бұрын
@@TECHSCHOOLGURU lol that's the easiest way i've ever heard someone explain it, thanks for that.
@Mona001-01g
@Mona001-01g 2 жыл бұрын
Are there any pre requisite video to this lesson 4 video that I can follow to understand the file contents of files generated with sqlc generate command?
@panggilmeiam
@panggilmeiam 11 ай бұрын
Damn bro! Thank you very much!!! I learn a lot!
@toledot
@toledot Жыл бұрын
sqlc v1.20.0 on MacOS will not generate the file named "account.sql.go". Is it correct? or do I do anything wrong?
@apramaysingh1291
@apramaysingh1291 Жыл бұрын
You have to add comments above your query file's instruction like so: #Here the "-- name: createUser' is the name of the function and ":one" indicates a returning of a single schema INSERT into users ( username, hashed_password, full_name, email ) VALUES ( ?1, ?2, ?3, ?4 ) RETURNING *; This way SqlC know how to actually generate the function
@nityanandagohain4154
@nityanandagohain4154 3 жыл бұрын
hands down, this is amazing. Thank you for making this
@TECHSCHOOLGURU
@TECHSCHOOLGURU 3 жыл бұрын
Glad you liked it!
@pakersmuch3705
@pakersmuch3705 4 ай бұрын
Love it! Thanks.
@kyphanquoc6824
@kyphanquoc6824 Жыл бұрын
I wonder a extension for .sql file of your vscode? I try install some extensions but it don't work (ex: SQL SERVER (mssql), Prettier SQL VSCODE). Please, help me :^^
@robertommmin5
@robertommmin5 Жыл бұрын
Gold Mine! Thank you
@afiqurrahman2884
@afiqurrahman2884 Жыл бұрын
I found this problem the postgresql engine requires cgo.please set CGO_ENABLED=1. I try export in makefile but still same
@HelloWorld-tn1tl
@HelloWorld-tn1tl Жыл бұрын
version 2 is preferred now.
@DrPanesar
@DrPanesar 3 жыл бұрын
Hi, I've used sqlc for the first time. I wonder can you use it to write your one more complex SQL statements such as If and multicolon operations. It does not build go functions then using the sqlc generate command. I wonder if you can write it manually and then have sqlc run your own function / code?
@TECHSCHOOLGURU
@TECHSCHOOLGURU 3 жыл бұрын
Hi Paramjit, sqlc can generate code for many different types of complex queries. You can try it on your own to see if it works for your specific case or not. In case it doesn't work, you can always write your own code using the standard db/sql library, and use it together with other functions generated by sqlc.
@budi0580
@budi0580 2 жыл бұрын
You should add sqlboiler into the list
@ThapakornTantirattanapong
@ThapakornTantirattanapong Жыл бұрын
MySQL is now usable?
@tradealert3802
@tradealert3802 2 жыл бұрын
Why not use sqlc to also handle the creation of the tables rather than using go migrate? Is there a way to also do migration with sqlc
@TECHSCHOOLGURU
@TECHSCHOOLGURU 2 жыл бұрын
Each tool has its own purpose. Sqlc was not designed to run DB migrations.
@akashrajvanshi6362
@akashrajvanshi6362 Жыл бұрын
What is diff btw this playlist and udemy course???
@TECHSCHOOLGURU
@TECHSCHOOLGURU Жыл бұрын
Hi Akash, Several people have asked this question in Tech School's discord server. discord.com/invite/BksFFXu Here's my answer: The content is the same. The only difference is, there are ads on KZbin videos, and on Udemy you will get a certificate of completion if you finish the course. I created Tech School with the purpose of providing free education, so anyone who can't afford to buy the course can still learn for free on KZbin. A lot of students who already watched youtube videos also purchased the course on Udemy as a way of supporting my mission.
@LewisFlorezRenza
@LewisFlorezRenza 3 ай бұрын
I can't installed on Windows, I got a message about dll library is not found. any solution?
@mcgam3r_tv809
@mcgam3r_tv809 Жыл бұрын
Amazing thanks brother!
@Llanilek
@Llanilek 2 жыл бұрын
How do you nest foreign key data into the sql query?
@viktorkot992
@viktorkot992 2 жыл бұрын
for windows docker run --rm -v "%cd%:/src" -w /src kjconroy/sqlc
@TECHSCHOOLGURU
@TECHSCHOOLGURU 2 жыл бұрын
Thank you!
@idirdev3729
@idirdev3729 4 жыл бұрын
Thanks for these great videos, Could you Pls show us the best way to make a bulk insert?
@TECHSCHOOLGURU
@TECHSCHOOLGURU 4 жыл бұрын
Great suggestion, Idir! I will try to make a video about it in the future.
@Nexjsdeveloper
@Nexjsdeveloper 2 жыл бұрын
Worth as gold. I don't now how can i thank you
@TECHSCHOOLGURU
@TECHSCHOOLGURU 2 жыл бұрын
Thank you! You can support us by liking & sharing the videos, or buy us a coffee. ^^
@pol3na
@pol3na 8 ай бұрын
Did anyone have issue with installing sqlc on linux? installed it using snap, when i typed sqlc version it said that command not found.
@xerofit1397
@xerofit1397 3 жыл бұрын
Hi Quang, thanks for the vid! sqlc is really amazing. I have a question though, when we're writing a query for generating sqlc code that involves more than one table and requires a join let's say table A and B, where is the best practice to store that query? A.sql, B.sql, or create a new A-B.sql (Sorry if the example lacks in context, I can't think of a real case). Thank you!
@TECHSCHOOLGURU
@TECHSCHOOLGURU 3 жыл бұрын
Hi Xerofit, I think where to store the query might depend on the context of the use-case. If the data you're querying is more of A (and just some additional data from B), then you can put it in A.sql (and vice-versa) For some specific use-case, you might even put the query in its own separate file if you want.
@xerofit1397
@xerofit1397 3 жыл бұрын
@@TECHSCHOOLGURU Thank you!
@introvert9639
@introvert9639 4 жыл бұрын
Sqlc doc doesn't says about writing native queries or complex queries including multiple table joins etc. ....any idea?
@TECHSCHOOLGURU
@TECHSCHOOLGURU 4 жыл бұрын
Good question! You can write any complex queries and generate code for it with sqlc. For example: -- name: GetEntryWithAccount :one SELECT e.*, a.owner, a.balance FROM entries as e JOIN accounts as a ON e.account_id = a.id WHERE e.id = $1 LIMIT 1;
@hafizmfadli
@hafizmfadli 2 жыл бұрын
this is amazing course !!!!!!!!!!!!
@TECHSCHOOLGURU
@TECHSCHOOLGURU 2 жыл бұрын
Thank you!
@muhammadsesay7487
@muhammadsesay7487 2 жыл бұрын
This is very amazing.
@devfun3032
@devfun3032 3 жыл бұрын
Hey bro. Sqlc release version 1.10. Now, What should version use? Thanks
@TECHSCHOOLGURU
@TECHSCHOOLGURU 3 жыл бұрын
Hey, I think you can go with the latest version.
@ddlelo8489
@ddlelo8489 4 жыл бұрын
Please make video to create a REST API continuing this process
@TECHSCHOOLGURU
@TECHSCHOOLGURU 4 жыл бұрын
Sure, Elo. Coming up next week!
@rizkimuntohary7350
@rizkimuntohary7350 2 жыл бұрын
Thankyou this is amazing
@TECHSCHOOLGURU
@TECHSCHOOLGURU 2 жыл бұрын
Thank you!
@jeremyjohns9615
@jeremyjohns9615 4 жыл бұрын
How much would be the difference if we use sqlx instead of sqlc? Because I am facing issues while installing sqlc on my computer (using Windows 10 Home). Can we still follow your tutorial while using sqlx?
@TECHSCHOOLGURU
@TECHSCHOOLGURU 4 жыл бұрын
Hi Jeremy, you can use sqlx if you want. The only difference is that if you use sqlx then you have to implement all the CRUD yourself, while if you use sqlc then they're automatically generated for you. If you have docker running on your Windows machine, then you can try this to generate sqlc code with docker run: github.com/kyleconroy/sqlc#docker docker run --rm -v $(pwd):/src -w /src kjconroy/sqlc generate
@jeremyjohns9615
@jeremyjohns9615 4 жыл бұрын
@@TECHSCHOOLGURU if the only difference is implementing the CRUD functionality, I will give it a go when I give up installing sqlc. I have pulled an image using docker pull, but when I run the docker run command, it shows an error. Do you need to replace "pwd" command with something else while running docker run?
@TECHSCHOOLGURU
@TECHSCHOOLGURU 4 жыл бұрын
Sorry I don't have a Windows machine so I don't know. It seems like developing on a Windows machine is not very easy. So perhaps you can try to switch to linux or mac instead?
@parvineyvazov
@parvineyvazov 2 жыл бұрын
You can run like this: docker run --rm -v "%cd%:/src" -w /src kjconroy/sqlc generate
@sajirmohd
@sajirmohd 3 жыл бұрын
I didnt have error even without 'go mod init' Why we are initializing go mod then?
@TECHSCHOOLGURU
@TECHSCHOOLGURU 3 жыл бұрын
Hi Sajir, That's a good question! The command `go mod init` creates a `go.mod` file to keep track of all external modules that your project depends on. So if you don't have any dependencies, then you probably don't need it. Besides, if you're writing a library module (like an open-source library for example), then it's very important that you run `go mod init` with a valid path from which your module can be downloaded by Go tools. You can read more about how to create a module here: golang.org/doc/tutorial/create-module
@daryalazareva9664
@daryalazareva9664 3 жыл бұрын
Hi! Thanks for the videos, they're amazing! I have an error while running 'sqlc init': Error: open sqlc.yaml: permission denied. Does anyone know how to fix it?
@daryalazareva9664
@daryalazareva9664 3 жыл бұрын
it helped to use sqlc inside a container running command docker run --rm -v $(pwd):/src -w /src kjconroy/sqlc init using ubuntu by the way
@faizulla5838
@faizulla5838 Жыл бұрын
Author is hear ? How work without GORM?
@milanpanic3755
@milanpanic3755 Ай бұрын
is this out of date in 08 Oct 2024 ?
@jcolindres001
@jcolindres001 2 жыл бұрын
Hi Sir, Excellent training! Do you know a tool / library like sqlc for generating graphql in go? Regards, JA
@pawelprusinski5251
@pawelprusinski5251 3 жыл бұрын
So it sounds that best way (speed) is to use std library and do 4 functions for CRUD.
@TECHSCHOOLGURU
@TECHSCHOOLGURU 3 жыл бұрын
Yeah, the best way for speed is to use sqlc to generate code that use standard db/sql package. We can also combine sqlc with other package like db/sql or db/sqlx for some cases that sqlc cannot generate codes (because the query is too complex for example)
@connorjcantrell
@connorjcantrell 3 жыл бұрын
@@TECHSCHOOLGURU This is an interesting concept. I am curious to see an example of this
@huunam1001
@huunam1001 Жыл бұрын
I have problem when query LIKE OR ILIKE with SLQC. Is any body here to help me. Other, I wonder if SQLC use normal SQL statement?
@TECHSCHOOLGURU
@TECHSCHOOLGURU Жыл бұрын
Yes, you can use LIKE or ILIKE as normal query. If you want to discuss more, feel free to join Tech School's Discord group to chat directly with me and other student: bit.ly/techschooldc
@XuanQuynhLexuanquynh
@XuanQuynhLexuanquynh 3 жыл бұрын
thank for sharing.
@TECHSCHOOLGURU
@TECHSCHOOLGURU 2 жыл бұрын
Thank you for watching!
@bishanrasaili7246
@bishanrasaili7246 2 жыл бұрын
After I write the query file and run the command 'sqlc generate', I get the error simple_bank\db\migration\000001_init_schema.up.sql:1:1: the PostgreSQL engine does not support Windows. Can someone please help me resolve this issue ?
@TECHSCHOOLGURU
@TECHSCHOOLGURU 2 жыл бұрын
Hi Bishan, Some of my students have successfully generated the code using sqlc on Windows with this approach: - Setup WSL and run the linux binary of sqlc like you would normally run an executable. docs.microsoft.com/en-us/windows/wsl/install-win10 Maybe you can give it a try? Or another approach using docker: First, pull the sqlc image. docker pull kjconroy/sqlc Then run the specific commands in the PowerShell as follows: docker run --rm -v ${pwd}:/src -w /src kjconroy/sqlc version docker run --rm -v ${pwd}:/src -w /src kjconroy/sqlc init docker run --rm -v ${pwd}:/src -w /src kjconroy/sqlc generate
@namhuynh126
@namhuynh126 4 жыл бұрын
R u Vietnamese. Thank u a lot for this video. It help me so much.
@TECHSCHOOLGURU
@TECHSCHOOLGURU 4 жыл бұрын
Yes I am. Thank you, Nam!
@TheSatyrical
@TheSatyrical 2 жыл бұрын
For Window users: if other options don't work, try downloading WSL2 kzbin.info/www/bejne/jH69eWyGn5eXjtk Make sure to redownload (onto WSL2) Go, GCC, and make. You can use Go to install sqlc and everything should work. Also, you can download windows terminals (the base WSL2 terminal isn't good). And try out zsh.
@TECHSCHOOLGURU
@TECHSCHOOLGURU 2 жыл бұрын
Thanks a lot for the tip, Atropolith!
@nitishsaini972
@nitishsaini972 2 жыл бұрын
But on windows, only MySQL is available and when you run sqlc generate it tells postgres not available for windows, any solution for that
@michaelumeokoli
@michaelumeokoli 2 жыл бұрын
@@nitishsaini972 I'm facing this same issue
@phongnguyenthanh9577
@phongnguyenthanh9577 Жыл бұрын
How to close database connection after done
@TECHSCHOOLGURU
@TECHSCHOOLGURU Жыл бұрын
Hi Phong, As per documentation of the database/sql package, we don't have to close the db connection: pkg.go.dev/database/sql#Open "The returned DB is safe for concurrent use by multiple goroutines and maintains its own pool of idle connections. Thus, the Open function should be called just once. It is rarely necessary to close a DB."
@edarha7183
@edarha7183 4 жыл бұрын
how to create connection pool between golang and sql?
@TECHSCHOOLGURU
@TECHSCHOOLGURU 3 жыл бұрын
Hi Edar Ha. That's a good question! Actually when you use the standard database/sql package, there's already a basic connection pool implemented under the hood. There are several methods you can use to control the number of connections and their lifetime, such as db.SetMaxIdleConns(N), db.SetMaxOpenConns(N), db.SetConnMaxLifetime(duration) I will definitely make a video about this in one of the lecture. For now, you can read more about it here: go-database-sql.org/connection-pool.html www.alexedwards.net/blog/configuring-sqldb golang.org/pkg/database/sql/#DB.SetConnMaxIdleTime
@edarha7183
@edarha7183 3 жыл бұрын
@@TECHSCHOOLGURU yep, thanks for your reply. What are you think about mongodb and driver mongodb in golang?
@angnat05
@angnat05 Жыл бұрын
Got an error error, generating code: source error: 99:14: expected ';', found sql (and 1 more errors) , thanks for kind help
@TECHSCHOOLGURU
@TECHSCHOOLGURU Жыл бұрын
Hi @angnat, I can't help without seeing your codes first. Can you share your codes on a GitHub repo? And perhaps you can join Tech School's discord channel to chat directly with me and other students: discord.com/invite/BksFFXu It's faster and much more convenient to communicate than KZbin comments.
@angnat05
@angnat05 Жыл бұрын
@@TECHSCHOOLGURU sure, would you mind to share invitation to discord?
@TECHSCHOOLGURU
@TECHSCHOOLGURU Жыл бұрын
yeah it's the link i posted in the previous comment: discord.com/invite/BksFFXu
@sajirt2069
@sajirt2069 3 жыл бұрын
After installing sqlc (verified with 'sqlc version'), i tried 'sqlc init' inside the project folder and i am getting an error, user@user-HP-Laptop-15-db0xxx:~/Projects/go-simplebank$ sqlc init Error: open : no such file or directory How to fix this?
@TECHSCHOOLGURU
@TECHSCHOOLGURU 3 жыл бұрын
Hi Sajir, This is really strange. I've never faced this error before. What version of sqlc are you using?
@sajirt2069
@sajirt2069 3 жыл бұрын
@@TECHSCHOOLGURU Thanks for the response sqlc version: v1.7.0 Also i didnt have error without 'go mod init' Why we are doing it?
@TECHSCHOOLGURU
@TECHSCHOOLGURU 3 жыл бұрын
Hey Sajir, I'm using slqc 1.6.0, so maybe there's something different. But someone pointed out in other comment that it may work if you run: sqlc init -f "sqlc.yaml" Maybe it's worth a try! Regarding your question about go mod init, I also answered in other comment, but will repost it again here: The command `go mod init` creates a `go.mod` file to keep track of all external modules that your project depends on. So if you don't have any dependencies, then you probably don't need it. Besides, if you're writing a library module (like an open-source library for example), then it's very important that you run `go mod init` with a valid path from which your module can be downloaded by Go tools. You can read more about how to create a module here: golang.org/doc/tutorial/create-module
@bittom
@bittom 2 жыл бұрын
## **Generate CRUD Golang code from SQL | Compare db/sql, gorm, sqlx & sqlc** 关键是是用 SQLC 替代自己手写命令做 CRUD,目前仅仅支持 postgre Mac 上用 homebrew 安装 在之前文件夹中使用 sqlc init 创建 yaml 文件,然后 code . 打开,根据 github 1.4.0 版本设置相关配置 关键在于修改路径,很多文件都放到 db 单一文件夹下面的许多其他文件 后面几个参数是有具体含义,但是现在没全理解的时候暂时不考虑,注意后面的 exact_table_name 仍然设置为 false 还是关键表示方法 ### Create 使用 sql generate,在新的文件夹生成后建立 .sql 数据库文件 在 github 文件当中找到 start 相关配置,注意此时 primary key 和其他 default 相关字段会自动生成,此处给出自己要做的内容就好 首次使用 sqlc generate 后,在 sqlc 文件夹生成三个文件,models.go 会自动生成数据库字段相关结构的文件,也是跟 yaml 文件规定的格式相关,比如 json 字段是否显示 注意此时的 db.go 文件使用 DBTX 相关操作,主要是用 db 或者 TX 来使用查询 最后看到 account.go 文件,其中数据结构和方法如何实现 go mod init 到 github,生成文件之后 go mod tidy 目前还没有各种外部包 注意不能修改 sqlc 生成的 go 文件,每次都是 sqlc generate 根据 .sql 生成新文件就行 ### Read 此处有 Get 和 List 两种方法,从 start 文件找到后根据自己数据库字段对应修改 ### Update 此处是不返回只是执行的 :exec 标签,但是最后还是加上 RETURN 可以返回相关信息 ### Delete 此处也是执行,但是结构比较简单
@mechtarin
@mechtarin 3 жыл бұрын
I used database/sql in my project if anyone with windows dont try to use sqlc it does not work what ever you do it works only with mysql and that's not a good idea to use experimental thing
@TECHSCHOOLGURU
@TECHSCHOOLGURU 3 жыл бұрын
I don't think what you said is true. SQLC works for Postgres, no mater if you're on Windows or not. You can try using Docker on Windows to generate the code: docs.sqlc.dev/en/latest/overview/install.html#docker
@mechtarin
@mechtarin 3 жыл бұрын
@@TECHSCHOOLGURU i have tried it gave me this error the PostgreSQL engine does not support Windows I used docker with docker run --rm -v ${pwd}:/src -w /src kjconroy/sqlc generate
@vedantshinde2277
@vedantshinde2277 3 жыл бұрын
-- name: ListTransfers :many SELECT * FROM transfers WHERE from_account_id = $1 OR to_account_id = $2 ORDER BY id LIMIT $3 OFFSET $4; filtering using 'AND' keyword instead of 'OR' would make more sense, no? -- name: ListEntries :many SELECT * FROM entries WHERE account_id = $1 ORDER BY id LIMIT $2 OFFSET $3; Is it okay if the entries are ordered in descending order of account_id?
@TECHSCHOOLGURU
@TECHSCHOOLGURU 3 жыл бұрын
1. It depends on the use case. If you just want to list all transfers from account 1 to account 2 then use AND. In this case, I want to list all transfers of the same account (either going in or out), so I use OR, and if you look at the TestListTransfer function, you will see that I pass the same account ID into the function. 2. This query list all entries of 1 specific account (in the WHERE clause), so it doesn't make sense to order by account id, since there is only 1 account ID in the result, right?
@vedantshinde2277
@vedantshinde2277 3 жыл бұрын
@@TECHSCHOOLGURU Sorry, I meant order by created_at in a descending fashion.
@davidontiveros2895
@davidontiveros2895 3 жыл бұрын
ughhh crap. Got stuck until this point. Using Windows 😥
@TECHSCHOOLGURU
@TECHSCHOOLGURU 3 жыл бұрын
Hi David, have you tried using Linux Subsystem on Windows 10? docs.microsoft.com/en-us/windows/wsl/install-win10
@davidontiveros2895
@davidontiveros2895 3 жыл бұрын
@@TECHSCHOOLGURU yes, actually I kept reading and saw the docker comment. So I am running sqlc on a container. It works fine. Thanks for replying and btw your videos are top notch! Congratulations!
@vedantshinde2277
@vedantshinde2277 3 жыл бұрын
@@davidontiveros2895 Hey... I'm getting an error while executing this command: "docker run --rm -v C:/Users/vedant/src -w /src kjconroy/sqlc generate" It either says that "error parsing sqlc.json: file does not exist" or schema.sql doesn't exist. I can barely find any help on forums and the documentation just sums it up in a line. Any help would be appreciated
@davidontiveros2895
@davidontiveros2895 3 жыл бұрын
@@vedantshinde2277 I ended up using the linux binary of sqlc like techschool mentioned. If you are running Windows you can setup WSL and run the binary like you would normally run an executable.
@vedantshinde2277
@vedantshinde2277 3 жыл бұрын
@@davidontiveros2895 I did that exact thing and it worked like a charm! Thank you!!
@mannerism6286
@mannerism6286 3 жыл бұрын
Has anyone come across 'panic: runtime error: invalid memory address or nil pointer dereference' after running make sqlc?
@bobnicholson1011
@bobnicholson1011 3 жыл бұрын
I have the same problem and cannot figure out what’s causing it. It occurs on “sqlc generate”. I’m on a mac trying to use 1.8 sqlc.
@mannerism6286
@mannerism6286 3 жыл бұрын
@@bobnicholson1011 i think you should check typos in the fields. My issue was the typo.
@bobnicholson1011
@bobnicholson1011 3 жыл бұрын
@@mannerism6286 I figured it out. Somehow I had commas at the end of my migration file names. It appears that it did not affect the migrations. But, it seems to cause a problem for "sqlc generate”. Thanks to your response, I continued looking.
@buacomgiadinh1
@buacomgiadinh1 4 жыл бұрын
Please make text bigger, thank you
@TECHSCHOOLGURU
@TECHSCHOOLGURU 4 жыл бұрын
Hi Ha Do, Thanks for the suggestion. It's quite readable to my eyes. Can you try to watch the video in HD or 4K to see if it's clearer to you?
@buacomgiadinh1
@buacomgiadinh1 4 жыл бұрын
TECH SCHOOL thank you for your answer, i use smart tv and iPhones to watch your video. Thank you for your great work
@jeremyjohns9615
@jeremyjohns9615 4 жыл бұрын
Did someone successfully installed sqlc on Windows? (using Windows 10 Home)
@TECHSCHOOLGURU
@TECHSCHOOLGURU 4 жыл бұрын
Perhaps you can try running sqlc with docker: github.com/kyleconroy/sqlc#docker
@vedantshinde2277
@vedantshinde2277 3 жыл бұрын
Did you manage to get it running? I'm getting an error while executing this command: "docker run --rm -v C:/Users/vedant/src -w /src kjconroy/sqlc generate" It either says that "error parsing sqlc.json: file does not exist" or schema.sql doesn't exist.
@kzm694
@kzm694 4 жыл бұрын
source code on github??
@TECHSCHOOLGURU
@TECHSCHOOLGURU 4 жыл бұрын
Hi Bardia, it's in the description: github.com/techschool/simplebank
@mechtarin
@mechtarin 3 жыл бұрын
the PostgreSQL engine does not support Windows yeey
@visa2256
@visa2256 3 жыл бұрын
yo I am having this prob rn lol
@TECHSCHOOLGURU
@TECHSCHOOLGURU 3 жыл бұрын
Hi guys, I think you can try using Docker to generate the code: docs.sqlc.dev/en/latest/overview/install.html#docker
@mechtarin
@mechtarin 3 жыл бұрын
@@TECHSCHOOLGURU i tried it does not work
@davidle5711
@davidle5711 4 жыл бұрын
Hi is sqlc not supported on Windows ? the first time I run go get github.com/kyleconroy/sqlc/cmd/sqlc it was okay. But when I run go install github.com/kyleconroy/sqlc/cmd/sqlc I got the error lfittl\pg_query_go\parser\include/port.h:17:10: fatal error: netdb.h: No such file or directory Anyone encounter the same error as me ?
@TECHSCHOOLGURU
@TECHSCHOOLGURU 4 жыл бұрын
Hi Leong, can you try to run sqlc using docker run as described here? github.com/kyleconroy/sqlc/#docker
@jeremyjohns9615
@jeremyjohns9615 4 жыл бұрын
@@TECHSCHOOLGURU I have pulled an image using docker pull, but when I run the docker run command, it shows an error. Do you need to replace "pwd" command with something else while running docker run?
@vedantshinde2277
@vedantshinde2277 3 жыл бұрын
Did you manage to get it running? I'm getting an error while executing this command: "docker run --rm -v C:/Users/vedant/src -w /src kjconroy/sqlc generate" It either says that "error parsing sqlc.json: file does not exist" or schema.sql doesn't exist.
@until_4ever362
@until_4ever362 4 жыл бұрын
sqlc does not support windows, unhappy
@TECHSCHOOLGURU
@TECHSCHOOLGURU 4 жыл бұрын
Hi Ezzz, sqlc is written in Go, so I'm pretty sure it can run on Windows. Can you try to install it using go get? go get github.com/kyleconroy/sqlc/cmd/sqlc Otherwise, you can still run it within a docker container: github.com/kyleconroy/sqlc#docker docker pull kjconroy/sqlc docker run --rm -v $(pwd):/src -w /src kjconroy/sqlc generate
@until_4ever362
@until_4ever362 4 жыл бұрын
@@TECHSCHOOLGURU go get or use git clone for go build,the same error: No such file or directory
@TECHSCHOOLGURU
@TECHSCHOOLGURU 4 жыл бұрын
How about run it with docker? docker run --rm -v $(pwd):/src -w /src kjconroy/sqlc generate Or perhaps try to build a binary of sqlc from source github.com/kyleconroy/sqlc#building, then use it to generate the codes?
@namhuynh126
@namhuynh126 4 жыл бұрын
U can try wsl2 for window. Wsl2 is very simple for install and it strong right now. I don't have install dual os anymore.
@LarryWEitel
@LarryWEitel 3 жыл бұрын
Has anyone resolves sqlc not running on windows?
@michaelumeokoli
@michaelumeokoli 2 жыл бұрын
Hello I keep getting this # package bankdb C:\Users\user\Desktop\GO-STUFF\BANK-APP\db\migration\000001_init_schema.up.sql:1:1: the PostgreSQL engine does not support Windows
@TECHSCHOOLGURU
@TECHSCHOOLGURU 2 жыл бұрын
Hi Mikey, On Windows, you can run sqlc using Docker as suggested in their documentation page: docs.sqlc.dev/en/latest/overview/install.html#docker
@aliadel1723
@aliadel1723 Жыл бұрын
anyone facing the problem of error parsing sqlc.yaml: yaml: unmarshal errors: line 2: field packages not found in type config.Config make: *** [Makefile:19: sqlc] Error 1 sloving with: version: "2" sql: - schema: "./db/migration/" queries: "./db/query/" engine: "postgresql" gen: go: package: "db" emit_json_tags: true out: "./db/sqlc" and makeFileDir := $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) sqlc: docker run --rm -v $(makeFileDir):/src -w /src kjconroy/sqlc generate
@BasicInfo-gg1lo
@BasicInfo-gg1lo 3 ай бұрын
my all query is running but insert query is not running what I do if am unable to make account
Happy birthday to you by Secret Vlog
00:12
Secret Vlog
Рет қаралды 6 МЛН
快乐总是短暂的!😂 #搞笑夫妻 #爱美食爱生活 #搞笑达人
00:14
朱大帅and依美姐
Рет қаралды 12 МЛН
Are ORMs Worth Using?
12:31
Ben Davis
Рет қаралды 15 М.
SQLC in Go - Auto-Generating Database Code in Golang
32:02
BugBytes
Рет қаралды 14 М.
I Would Never Use an ORM, by Matteo Collina
25:26
JetBrains
Рет қаралды 71 М.
Why Golang Developers HATE Gorm…
4:07
Golang Dojo
Рет қаралды 35 М.
How To Use The Context Package In Golang?
17:03
Anthony GG
Рет қаралды 66 М.
sqlc - Generate your Go database code with ease
11:18
Go Web Examples
Рет қаралды 15 М.
Making It FAST - 1 Billion Row Challenge in Go
31:54
ThePrimeTime
Рет қаралды 159 М.