Giter VIP home page Giter VIP logo

Comments (6)

rittneje avatar rittneje commented on May 18, 2024 1

(Note: I do not use gorm, and am only familiar with SQLite and the mattn wrapper.)

In SQLite, for historical reasons whether foreign keys are enabled is a property of an individual database connection, NOT the database itself. Consequently the fact that your DB Browser shows foreign keys enabled is irrelevant.

In order to enable foreign keys on your application's database connections, you will likely have to set a parameter in the DSN. Should be sqlite.Open("file:" + dbName+".db?_fk=1"). See the mattn docs for more information.

from sqlite.

rams3sh avatar rams3sh commented on May 18, 2024

@rittneje Thanks. Sure.

I understand that foreign key in sqlite is connection specific. The screenshot was shared just to show that it worked from the browser and not from gorm. This issue is very specific to gorm and not sqlite I feel.

sqlite.Open("file:" + dbName+".db?_fk=1") according to my understanding is to be used only if you are dealing with sqlite package directly without an intermediate ORM interface. This connection string is expected to be handled by the gorm instead based on the tag provided in the struct field which is being done alrite in case of postgresql but no sqlite.

Hence this issue.

from sqlite.

rittneje avatar rittneje commented on May 18, 2024

I don't see any logic in the source code to manipulate the connection string. It just passes it verbatim. For Postgres it works because I don't think you can disable foreign keys anyway.

It would be nice if gorm just always enabled foreign keys, regardless of whether your schema references them. But that might be a breaking change, for the same reason that SQLite itself considered it a breaking change.

BTW, another workaround for now is to compile with the sqlite_foreign_keys tag, as that will enable foreign keys by default.

from sqlite.

rams3sh avatar rams3sh commented on May 18, 2024

It would be nice if gorm just always enabled foreign keys, regardless of whether your schema references them. But that might be a breaking change, for the same reason that SQLite itself considered it a breaking change.

I think gorm can have foreign key enforce option applicable / considered only for sqlite with some default value handling for backward compatibility, and user can explicitly set that flag during initialisation.

Anyway I am just speculating and throwing of ideas from my head.

from sqlite.

BinaryTENSHi avatar BinaryTENSHi commented on May 18, 2024

After updating gorm and gorm/sqlite I also stumbled upon this problem. Not including '?_fk=1' is only part of the problem as adding it to the example code makes no difference: create still does not return an error.

I think the other part of the problem is closely related to mattn/go-sqlite3#986 (comment). When using a version of gorm/sqlite with RETURNING support (and only inserting a single struct), gorm itself calls rows.Next() once, scans the result but does not call rows.Next() again, resulting in the missing error. The fix in sqlite3 itself (https://sqlite.org/src/info/a818ba2ed635b91e) may resolve this issue, but it's not released yet. I'll try to patch it by hand and reverify the problem.

EDIT: Manually updating mattn/go-sqlite3 with the newest prerelease amalgamation code fixes this problem. Guess we have to wait for sqlite 3.38.0. (:

Otherwise, downgrading to 1.1.6 would also be an option: this is the latest Version without RETURNING support.

from sqlite.

jinzhu avatar jinzhu commented on May 18, 2024

https://sqlite.org/foreignkeys.html

If it still doesn't work, try to run PRAGMA foreign_keys = ON, the case should be tested in github.com/go-gorm/gorm

from sqlite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.