-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Allow ID autoincrement in SQLite (#82)
- Loading branch information
Showing
8 changed files
with
39 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
""" | ||
SQLAlchemy + SQLite does not support the autoincrement feature for BigInteger columns. | ||
This file provides a class as a workaround for this problem: | ||
It uses a normal Integer column in SQLite and a BigInteger column otherwise. | ||
SQLite Integer columns can autoincrement, but they are limited to 2^63-1. | ||
See https://stackoverflow.com/a/23175518/4306257 for more information. | ||
""" | ||
|
||
from sqlalchemy import BigInteger | ||
from sqlalchemy.dialects import postgresql, mysql, sqlite | ||
|
||
# Solution from https://stackoverflow.com/a/23175518/4306257 | ||
BigIntegerWithAutoincrement = BigInteger() | ||
BigIntegerWithAutoincrement = BigIntegerWithAutoincrement.with_variant(postgresql.BIGINT(), 'postgresql') | ||
BigIntegerWithAutoincrement = BigIntegerWithAutoincrement.with_variant(mysql.BIGINT(), 'mysql') | ||
BigIntegerWithAutoincrement = BigIntegerWithAutoincrement.with_variant(sqlite.INTEGER(), 'sqlite') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
from sqlalchemy import ForeignKey, BigInteger, Column, String | ||
from sqlalchemy import ForeignKey, Column, String | ||
from sqlalchemy.orm import relationship | ||
|
||
from athena.database import Base | ||
from .db_submission import DBSubmission | ||
from .big_integer_with_autoincrement import BigIntegerWithAutoincrement | ||
|
||
|
||
class DBProgrammingSubmission(DBSubmission, Base): | ||
__tablename__ = "programming_submissions" | ||
repository_url: str = Column(String, nullable=False) # type: ignore | ||
|
||
exercise_id = Column(BigInteger, ForeignKey("programming_exercises.id", ondelete="CASCADE"), index=True) | ||
exercise_id = Column(BigIntegerWithAutoincrement, ForeignKey("programming_exercises.id", ondelete="CASCADE"), index=True) | ||
|
||
exercise = relationship("DBProgrammingExercise", back_populates="submissions") | ||
feedbacks = relationship("DBProgrammingFeedback", back_populates="submission") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
from sqlalchemy import Column, BigInteger, JSON | ||
from sqlalchemy import Column, JSON | ||
|
||
from .model import Model | ||
from .big_integer_with_autoincrement import BigIntegerWithAutoincrement | ||
|
||
|
||
class DBSubmission(Model): | ||
id = Column(BigInteger, primary_key=True, index=True, nullable=False) | ||
id = Column(BigIntegerWithAutoincrement, primary_key=True, index=True, autoincrement=True,) | ||
meta = Column(JSON, nullable=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
from sqlalchemy import ForeignKey, BigInteger, Column, String | ||
from sqlalchemy import ForeignKey, Column, String | ||
from sqlalchemy.orm import relationship | ||
|
||
from athena.database import Base | ||
from .db_submission import DBSubmission | ||
from .big_integer_with_autoincrement import BigIntegerWithAutoincrement | ||
|
||
|
||
class DBTextSubmission(DBSubmission, Base): | ||
__tablename__ = "text_submissions" | ||
text: str = Column(String, nullable=False) # type: ignore | ||
language: str = Column(String, nullable=True) # type: ignore | ||
|
||
exercise_id = Column(BigInteger, ForeignKey("text_exercises.id", ondelete="CASCADE"), index=True) | ||
exercise_id = Column(BigIntegerWithAutoincrement, ForeignKey("text_exercises.id", ondelete="CASCADE"), index=True) | ||
|
||
exercise = relationship("DBTextExercise", back_populates="submissions") | ||
feedbacks = relationship("DBTextFeedback", back_populates="submission") |