I want to make the auth_user.email case-insensitive unique, nullable and default null. The following almost works:
from django.db.models.signals import post_syncdb
import app.models
SQLITE_AUTH_REFORM = [
"PRAGMA writable_schema = 1;",
"""UPDATE SQLITE_MASTER SET SQL =
'CREATE TABLE auth_user (
"id" integer NOT NULL PRIMARY KEY,
"username" varchar(30) NOT NULL UNIQUE,
"first_name" varchar(30) NOT NULL,
"last_name" varchar(30) NOT NULL,
"email" varchar(75) DEFAULT NULL,
"password" varchar(128) NOT NULL,
"is_staff" bool NOT NULL,
"is_active" bool NOT NULL,
"is_superuser" bool NOT NULL,
"last_login" datetime NOT NULL,
"date_joined" datetime NOT NULL
)' WHERE NAME = 'auth_user';""",
"PRAGMA writable_schema = 0;",
]
def post_syncdb_callback(sender, **kwargs):
from django.db import connections
from django.conf import settings
cursor = connections['default'].cursor()
if 'sqlite' in settings.DATABASES['default']['ENGINE']:
for stmt in SQLITE_AUTH_REFORM:
cursor.execute(stmt)
cursor.execute(
"CREATE UNIQUE INDEX IF NOT EXISTS auth_user_email_unique "
"ON auth_user (email COLLATE NOCASE);"
)
else: # Oracle
cursor.execute(
"CREATE UNIQUE INDEX auth_user_email_unique "
"ON auth_user (upper(email));"
)
cursor.cursor.close()
post_syncdb.connect(post_syncdb_callback, sender=app.models)
I can
User.objects.create(username=str(random.random()), email=None)
To my heart's content. And also,
User.objects.create(username=str(random.random()), email='Foo')
User.objects.create(username=str(random.random()), email='foo')
...
IntegrityError: column email is not unique
The only problems is that the DEFAULT NULL does not seem to work: User.objects.create(username=str(random.random())) creates a user with an empty-string email.
However, in a unit-test, I believe something is going on that prevents the post-syncdb hook from working:
class DjangoUserTest(TestCase):
def test_unique_nullable_email(self):
import IPython; IPython.embed()
u1 = User.objects.create(username="u1", email=None)
u2 = User.objects.create(username="u2", email=None)
I can drop into the ipython shell and see that the table has been apparently modified:
In [1]: from django.db import connection
In [2]: c = connection.cursor()
In [3]: r = c.execute("select `sql` from sqlite_master WHERE tbl_name = 'auth_user';")
In [4]: r.fetchall()
Out[4]:
[(u'CREATE TABLE auth_user (\n "id" integer NOT NULL PRIMARY KEY,\n "username" varchar(30) NOT NULL UNIQUE,\n "first_name" varchar(30) NOT NULL,\n "last_name" varchar(30) NOT NULL,\n "email" varchar(75) DEFAULT NULL,\n "password" varchar(128) NOT NULL,\n "is_staff" bool NOT NULL,\n "is_active" bool NOT NULL,\n "is_superuser" bool NOT NULL,\n "last_login" datetime NOT NULL,\n "date_joined" datetime NOT NULL\n)',),
(None,),
(u'CREATE UNIQUE INDEX auth_user_email_unique ON auth_user (email COLLATE NOCASE)',)]
However, upon trying to do the creates, I get, IntegrityError: auth_user.email may not be NULL. How did this happen when the select sql from sqlite_master WHERE tbl_name = 'auth_user'; clearly says "email" varchar(75) DEFAULT NULL. I feel like I just need to commit the post_syncdb stuff or sth. Any ideas?
UPDATE: No amount of connection.commit(), cursor.close() helps, using TransactionTestCase does not help.
Related
Take a SQL table defined by:
CREATE TABLE `raw_dummy_code` (
`code` int DEFAULT NULL,
`description` text COLLATE utf8_unicode_ci,
`datestart` date DEFAULT NULL,
`dateend` date DEFAULT NULL,
UNIQUE KEY `code` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
and a R data frame defined by:
raw_dummy_code <- data.frame(code = c(4, 4L, 4L),
datestart = c("2001-01-01", "2002-01-01", "2002-01-01"),
dateend = c("2001-12-31", "2500-01-01", "2500-01-01"),
description = c("old","recent","recent"),
stringsAsFactors = FALSE)
When I try to insert the data frame in the database :
con <- RMySQL::dbConnect(RMySQL::MySQL(), dbname = "test")
RMySQL::dbWriteTable(con, "raw_dummy_code", raw_dummy_code, row.names = FALSE, append = TRUE)
RMySQL::dbDisconnect(con)
RMySQL::dbWriteTable inserts only one row in the database. This is normal, since there are duplicated rows, but the problem is that there is no error message returned.
How to get the error message from MySQL when trying to enter a duplicated key?
I have a many-to-one relationship as below (I have removed columns that do not contribute to this discussion):
#Entity
#SecondaryTable(name = "RecordValue", pkJoinColumns = {
#PrimaryKeyJoinColumn(name = "RECORD_ID", referencedColumnName = "RECORD_ID") })
Class Record {
#Id
#Column(name = "RECORD_ID")
long recordId;
#OneToMany(mappedBy="key")
Set<RecordValue> values;
}
#Entity
class RecordValue {
#EmbeddedId
RecordValuePK pk;
#Column
long value;
#ManyToOne
#MapsId("recordId")
private Record key;
}
#Embeddable
class RecordValuePK {
#Column(name = "RECORD_ID")
#JoinColumn(referencedColumnName = "RECORD_ID", foreignKey = #ForeignKey(name = "FK_RECORD"))
long recordId;
#Column(name = "COLLECTION_DATE")
LocalDate collectionDate;
}
When hibernate creates tables, the RecordValue table has primary key consisting of only RECORD_ID and NOT COLLECTION_DATE.
What could be the problem?
Hibernate debug log shows the following:
DEBUG - Forcing column [collection_date] to be non-null as it is part of the primary key for table [recordvalue]
DEBUG - Forcing column [key_record_id] to be non-null as it is part of the primary key for table [recordvalue]
DEBUG - Forcing column [record_id] to be non-null as it is part of the primary key for table [recordvalue]
.
.
Hibernate:
create table Record (
RECORD_ID bigint not null,
primary key (RECORD_ID)
)
Hibernate:
create table RecordValue (
COLLECTION_DATE date not null,
VALUE bigint not null,
key_RECORD_ID bigint not null,
RECORD_ID bigint not null,
primary key (RECORD_ID)
)
Removing the #SecondaryTable specification has resolved this issue. The #SecondaryTable specification was forcing both tables to have the same the primary key. The found this solution after reading this blog:
https://antoniogoncalves.org/2008/05/20/primary-and-secondary-table-with-jpa.
I wrote the following code and I'm trying to make an install to create the databases and tables for the SQL but I can't figure out why it's not creating the databases or the tables.
CONFIG.PHP
<?php
// Database Details
$host = 'localhost';
$user = 'root';
$password = '';
//Install Settings
$root_username = 'root';
$root_password = 'password';
// Install Settings Advanced - Do Not Modify For Default Settings
$database = 'Sims2';
?>
INSTALL.PHP
<?php
include 'config.php';
// Remove any instance of .mysqli_connect_error() before publishing
$conn = mysqli_connect($host,$user,$password);
if (!$conn) {die("Connection failed: ".mysqli_connect_error());};
$sql = "CREATE DATABASE Sims2";
if (!$conn){die("Error creating database: ".mysqli_connect_error());};
$sql = "CREATE TABLE TEACHERS (
id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
uid VARCHAR(128) NOT NULL,
pwd VARCHAR(128) NOT NULL
);";
if(!$conn){die("Error creating table: ".mysqli_connect_error());};
$sql = "CREATE TABLE STUDENTS (
id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
first VARCHAR(64) NOT NULL,
last VARCHAR(64) NOT NULL,
gender VARCHAR(6) NOT NULL,
dob VARCHAR(10) NOT NULL,
semail VARCHAR(64) NOT NULL,
address VARCHAR(120) NOT NULL,
phone VARCHAR(15) NOT NULL,
tutor VARCHAR(64) NOT NULL
);";
if(!$conn){die("Error creating table: ".mysqli_connect_error());};
$sql = "INSERT INTO TEACHERS (uid, pwd) VALUES ($root_username,$root_password)";
if(!$conn){die("Error creating root account: ".mysqli_connect_error());};
echo "Done";
?>
Any help would be appreciated
I want to input a query to check the database for duplicate when inserting data into the database so it would prevent the activity Name from being entered more than once in a database
function insertQueryDB(tx) {
var myDB = window.openDatabase("test", "1.0", "Test DB", 1000000);
tx.executeSql('CREATE TABLE IF NOT EXISTS dataEntryTb (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, activityName TEXT NOT NULL, location TEXT NOT NULL, time NOT NULL, date NOT NULL, reporter NOT NULL)');
var an = document.forms["myForm"]["activityName"].value;
var l = document.forms["myForm"]["location"].value;
var t = document.forms["myForm"]["time"].value;
var d = document.forms["myForm"]["date"].value;
var r = document.forms["myForm"]["reporter"].value;
var query = 'INSERT INTO dataEntryTb ( activityName, location, time, date, reporter) VALUES ( "'+an+'", "'+l+'", "'+t+'", "'+d+'", "'+r+'")';
navigator.notification.alert("Retrieved the following: Activity Name="+an+" and Location="+l);
tx.executeSql(query,[]);
}``
Create the table with name being unique:
CREATE TABLE IF NOT EXISTS dataEntryTb (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
activityName TEXT NOT NULL UNIQUE,
location TEXT NOT NULL,
time NOT NULL, date NOT NULL,
reporter NOT NULL
);
Then the database will return an error if the name is already in the table.
I'm trying to update my user table, which my code does. but for some reason it keeps executing the else statement.
in the documentation it states that updateByPk should return the number of rows being updated. Which should be 1. what am i missing here? and how do i check if the table has been updated successfully?
if (User::model()->updateByPk($model->id, array("last_login"=> Shared::timeNow())))
{
Yii::app()->user->login($identity, $duration);
echo json_encode(array('error' => false, 'success' => url(app()->users->getHomeUrl())));
Yii::app()->end();
}
else {
echo json_encode(array('error' => 'Could not update user info', 'code' => 'auth'));
Yii::app()->end();
}
my table schema is this
CREATE TABLE IF NOT EXISTS `user` (
`id` int(10) unsigned NOT NULL,
`username` varchar(100) DEFAULT NULL,
`email` varchar(255) NOT NULL,
`first_name` varchar(45) NOT NULL,
`last_name` varchar(45) NOT NULL,
`gender` char(1) DEFAULT NULL,
`birthday` date DEFAULT '0000-00-00',
`address` varchar(255) DEFAULT NULL,
`city` varchar(45) DEFAULT NULL,
`state` varchar(45) DEFAULT NULL,
`website` text,
`postal_code` varchar(45) DEFAULT NULL,
`phone` varchar(45) DEFAULT NULL,
`password` varchar(63) DEFAULT NULL,
`activate` varchar(63) NOT NULL DEFAULT '1',
`create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_login` datetime DEFAULT NULL,
`password_reset` int(11) unsigned DEFAULT NULL,
`admin` tinyint(1) unsigned NOT NULL DEFAULT '0',
`email_verified` tinyint(1) unsigned DEFAULT NULL,
`login_disabled` tinyint(1) unsigned NOT NULL DEFAULT '0',
`oauth_id` bigint(20) DEFAULT NULL,
`oauth_username` varchar(255) DEFAULT NULL,
`oauth_provider` varchar(10) DEFAULT NULL,
`oauth_email` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
You should check $model exits first otherwise updateByPk can return 0
if ($model && User::model()->updateByPk($model->id, array("last_login"=> Shared::timeNow())))
Your table does not specify a primary key. Yet you update by primary key.
You have a few choices.
Change your table structure to include a primary key
Use another update method. For example, Update() allow you to pass a criteria object.
Know it's old, but : https://www.php.net/manual/en/pdostatement.rowcount.php
If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.
And Yii1 use rowCount
Model can be not updated because last_login attribute has not valid value.
Try get model errors ($model->errors()) or check model by $model->validate();
Why you not implement this as follows:
$model->last_login = Shared::timeNow();
if ($model->save())
{
Yii::app()->user->login($identity, $duration);
echo json_encode(array('error' => false, 'success' => url(app()->users->getHomeUrl())));
Yii::app()->end();
}
else {
echo json_encode(array('error' => 'Could not update user info', 'code' => 'auth'));
Yii::app()->end();
}