-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow create table with array field constraints #1470
Allow create table with array field constraints #1470
Conversation
36e45d9
to
807fa2e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does only work with Mysql? The test seems to imply that. If that's the case, shouldn't this be in the MySqli/Forge class?
Perhaps. It is intended to work with any driver. I am still not aware of Data Types in other DBMS besides MySQL, PostgreSQL and SQLite. I found only the MySQL ENUM and SET Data Types allowing a list of values so far. So testing only once. |
But these are the queries: -- crate the type "mood" as ENUM
CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
-- create table using the "mood" - ENUM is not used inside CRATE TABLE query
CREATE TABLE person (
name text,
current_mood mood
); SQLite CHECK seems to me to be a hack to emulate MySQL ENUM. Thinking about doing something to not escape constraints like this, if the driver is SQLite: $db_columns = array(
'status' => array(
'type' => 'TEXT',
'constraint' => 'CHECK( status IN ("locked","unlocked") )',
'default' => 'unlocked',
'comment' => 'Status of asset (Locked or Unlocked)'
)
); |
Or, convert the type ENUM to TEXT and the CHECK in the constraint... It allow the same code works in mysql and sqlite |
The postgres "created type" can be used in other tables/fields too. |
Oh, good point about Postgres. And you're right that Check is a hack. I only mentioned it because it looked like it would have been implemented with an array. |
Can be. But the user also can: $db_columns = array(
'status' => array(
'type' => 'TEXT CHECK( status IN ("locked","unlocked") )',
)
); Or we allow CodeIgniter make the conversion: // sqlite: status TEXT CHECK( status IN ('unlocked','locked') )
$db_columns = array(
'status' => array(
'type' => 'ENUM',
'constraint' => ['unlocked', 'locked'],
)
); The same PHP code would works with mysql and sqlite |
for this, I like letting CI convert it. The most common use case I see for SQLite on the web is running tests, even though the web apps are actually using a different engine, like Mysql or Postgres in production. Converting it on the fly would be one less pain when trying to test your app. And, as you said, they can use check for other things as a text item. Would you mind doing that conversion? |
Yes. Also being able to "create the type" for postgres on the fly. But I fear that this will lead to collisions. |
I agree - and since creating the type is done outside of the create table I wouldn't worry about that one. Just sqlite. |
Description
See #976
Testing only ENUM in MySQLi - but all constraints set as array will be escaped and imploded, works with MySQL SET Data Type too.
Checklist: