casbin / gorm-adapter

GORM adapter for Casbin, see extended version of GORM Adapter Ex at: https://github.com/casbin/gorm-adapter-ex

Home Page:https://github.com/casbin/casbin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Native Go SQLite implementation

TJM opened this issue ยท comments

I propose that sqlite should be supported, perhaps using this implementation: "github.com/glebarez/sqlite"

REF: https://gorm.io/docs/connecting_to_the_database.html#SQLite

@TJM can you make a PR to support it?

@hsluoyz I tried, but failed. :( I use sqlite for local development. I have tried with both the 'c' implementation and this golang implementation. They both work fine for my application, which uses gorm, but I get an error about "id" fields when I try to use it for casbin. The funny part is that it works fine the first time you start, but the second time you start the app (where the database has already been populated) and it tries to create the enforcer, this error comes up:

.../vendor/github.com/casbin/gorm-adapter/v3/adapter.go:373 SQL logic error: duplicate column name: id (1)
[0.017ms] [rows:0] ALTER TABLE `casbin_rule` ADD `id` integer

.../middleware.createEnforcer() failed to initialize casbin gorm adapter: SQL logic error: duplicate column name: id (1) 
exit status 1

SO, it doesn't do any good to add it to the (internal?) supported databases. There is apparently some other problem relating to "id" column. Honestly, I don't think that this code should have any direct support for databases. It should just take the gorm.DB and use whatever gorm supports. :)

... that way I could reduce the amount of imported code for the other N databases that I have no intention of using :)

~tommy

@TJM I found the bug was because of this line of code.

if err := a.db.AutoMigrate(t); err != nil {

See AutoMigrate bug. Since glebarez/sqlite is too backward and has not been updated, this PR cannot be implemented at present.

Regarding your second question, I don't quite understand what it means. If you have a better idea, you can open a new PR

commented

@TJM I found the bug was because of this line of code.

if err := a.db.AutoMigrate(t); err != nil {

See AutoMigrate bug. Since glebarez/sqlite is too backward and has not been updated, this PR cannot be implemented at present.
Regarding your second question, I don't quite understand what it means. If you have a better idea, you can open a new PR

Actually it is not falling backwards.
Here's the diff between fork and the original repo glebarez/sqlite@master...go-gorm:master
The only thing is deps, which are already up to date in fork.

commented

@TJM does your case fail with both CGo and pure-Go implementations? Or is it just Pure-Go fails ?

@TJM I found the bug was because of this line of code.

if err := a.db.AutoMigrate(t); err != nil {

See AutoMigrate bug. Since glebarez/sqlite is too backward and has not been updated, this PR cannot be implemented at present.
Regarding your second question, I don't quite understand what it means. If you have a better idea, you can open a new PR

Actually it is not falling backwards. Here's the diff between fork and the original repo glebarez/sqlite@master...go-gorm:master The only thing is deps, which are already up to date in fork.

@glebarez Yes, you are right, I ignore that. If so, when this bug is resolved I'd be gald to implement our functionality based on it.

@TJM does your case fail with both CGo and pure-Go implementations? Or is it just Pure-Go fails ?

@glebarez I tested it. Fail with both CGo and pure-Go implementations.

commented

@TJM does your case fail with both CGo and pure-Go implementations? Or is it just Pure-Go fails ?

@glebarez I tested it. Fail with both CGo and pure-Go implementations.

created an issue go-gorm/gorm#5282 to the upstream.

commented

I fixed this issue in https://github.com/glebarez/sqlite
Please do go get -u github.com/glebarez/sqlite@v1.4.3
and try to reproduce.

You guys rock! Thanks!

I am passing it a glebarez/sqlite *gorm.DB, and it is working great!

I fixed this issue in https://github.com/glebarez/sqlite Please do go get -u github.com/glebarez/sqlite@v1.4.3 and try to reproduce.

@glebarez thanks. It works.

๐ŸŽ‰ This issue has been resolved in version 3.6.0 ๐ŸŽ‰

The release is available on GitHub release

Your semantic-release bot ๐Ÿ“ฆ๐Ÿš€