Skip to content
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

AutoIncrement column value cannot be set. #1447

Closed
jiriadamec opened this issue Sep 22, 2017 · 2 comments
Closed

AutoIncrement column value cannot be set. #1447

jiriadamec opened this issue Sep 22, 2017 · 2 comments
Assignees
Labels
Milestone

Comments

@jiriadamec
Copy link

jiriadamec commented Sep 22, 2017

DBFlow Version: 4.1.1
Issue Kind (Bug, Question, Feature): Bug

Description:
When I set a value to autoincrement column and try to save the model an exception occurs.
I created sample that simulates the error:

@Table(database = TestDb.class) 
public class TestModel extends BaseModel {

	@PrimaryKey(autoincrement = true)
	private int modelId;
	@Column
	private String name;


	public int getModelId() {
		return modelId;
	}


	public void setModelId(int modelId) {
		this.modelId = modelId;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}
}
@Test
public void testDb() {
	TestModel testModel = new TestModel();
	testModel.setModelId(1);
	testModel.setName("name");

	testModel.save();

	assertEquals(1, SQLite.select().from(TestModel.class).queryList().size());
}

java.lang.IllegalArgumentException: Cannot bind argument at index 2 because the index is out of range. The statement has 1 parameters.

@jiriadamec
Copy link
Author

jiriadamec commented Sep 22, 2017

I think the problem is in AutoIncrementModelSaver:

@Override
    public synchronized long insert(@NonNull TModel model,
                                    @NonNull DatabaseStatement insertStatement,
                                    @NonNull DatabaseWrapper wrapper) {
        if (getModelAdapter().hasAutoIncrement(model)) {
            getModelAdapter().bindToStatement(insertStatement, model);
        } else {
            getModelAdapter().bindToInsertStatement(insertStatement, model);
        }
        long id = insertStatement.executeInsert();
        if (id > INSERT_FAILED) {
            getModelAdapter().updateAutoIncrement(model, id);
            NotifyDistributor.get().notifyModelChanged(model, getModelAdapter(), BaseModel.Action.INSERT);
        }
        return id;
    }

the insertStatement does not contain modelId column, but the adapter tries to bind it.
my fix proposal is:

@Override
    public synchronized long insert(@NonNull TModel model,
                                    @NonNull DatabaseStatement insertStatement,
                                    @NonNull DatabaseWrapper wrapper) {
        return insert(model, wrapper);
    }

agrosner added a commit that referenced this issue Oct 15, 2017
@agrosner
Copy link
Owner

fixed for 4.1.2.

@agrosner agrosner added the bug label Oct 15, 2017
@agrosner agrosner self-assigned this Oct 15, 2017
@agrosner agrosner added this to the 4.1.2 milestone Oct 15, 2017
@agrosner agrosner mentioned this issue Oct 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants